Skip to content

Commit 4bd715e

Browse files
author
Shahzod Shafizod
committed
fixing comments
1 parent 4f9c73e commit 4bd715e

File tree

6 files changed

+18
-615
lines changed

6 files changed

+18
-615
lines changed

challenge-1/submissions/shahzodshafizod/solution-template_test.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

challenge-2/submissions/shahzodshafizod/solution-template_test.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

challenge-3/submissions/shahzodshafizod/solution-template.go

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"sort"
65
"sync"
76
)
87

@@ -20,66 +19,49 @@ type Manager struct {
2019
}
2120

2221
// AddEmployee adds a new employee to the manager's list.
23-
func (m *Manager) AddEmployee(e Employee) { // Time: O(n)
22+
func (m *Manager) AddEmployee(e Employee) { // O(1) or O(n)
2423
m.mu.Lock()
2524
defer m.mu.Unlock()
26-
// sort.Slice(m.Employees, func(i, j int) bool {
27-
// return m.Employees[i].ID < m.Employees[j].ID
28-
// })
29-
idx := m.search(e.ID)
30-
if idx == -1 {
31-
idx = len(m.Employees)
32-
}
33-
m.Employees = append(m.Employees, Employee{})
34-
copy(m.Employees[idx+1:], m.Employees[idx:])
35-
m.Employees[idx] = e
25+
m.Employees = append(m.Employees, e) // O(1) or O(n)
3626
m.totalSalary += e.Salary
3727
}
3828

3929
// RemoveEmployee removes an employee by ID from the manager's list.
40-
func (m *Manager) RemoveEmployee(id int) bool { // Time: O(n)
30+
func (m *Manager) RemoveEmployee(id int) bool { // O(n)
4131
m.mu.Lock()
4232
defer m.mu.Unlock()
43-
index := m.search(id) // O(log n)
44-
if index != -1 {
45-
m.totalSalary -= m.Employees[index].Salary
46-
m.Employees = append(m.Employees[:index], m.Employees[index+1:]...) // O(n)
47-
return true
33+
for index := range m.Employees {
34+
if m.Employees[index].ID == id {
35+
m.totalSalary -= m.Employees[index].Salary
36+
m.Employees = append(m.Employees[:index], m.Employees[index+1:]...)
37+
return true
38+
}
4839
}
4940
return false
5041
}
5142

5243
// GetAverageSalary calculates the average salary of all employees.
53-
func (m *Manager) GetAverageSalary() float64 { // Time: O(1)
44+
func (m *Manager) GetAverageSalary() float64 { // O(1)
5445
m.mu.RLock()
5546
defer m.mu.RUnlock()
56-
if n := len(m.Employees); n > 0 {
57-
return m.totalSalary / float64(n)
47+
if len(m.Employees) == 0 {
48+
return 0
5849
}
59-
return 0
50+
return m.totalSalary / float64(len(m.Employees))
6051
}
6152

6253
// FindEmployeeByID finds and returns an employee by their ID.
63-
func (m *Manager) FindEmployeeByID(id int) *Employee { // Time: O(log n)
54+
func (m *Manager) FindEmployeeByID(id int) *Employee { // O(n)
6455
m.mu.RLock()
6556
defer m.mu.RUnlock()
66-
index := m.search(id)
67-
if index != -1 {
68-
return &m.Employees[index]
57+
for index := range m.Employees {
58+
if m.Employees[index].ID == id {
59+
return &m.Employees[index]
60+
}
6961
}
7062
return nil
7163
}
7264

73-
func (m *Manager) search(id int) int { // Time: O(log n)
74-
index := sort.Search(len(m.Employees), func(i int) bool {
75-
return m.Employees[i].ID >= id
76-
}) // sort.Search returns an index where a new item could be inserted, so we do not receive -1 or an error
77-
if index < len(m.Employees) && m.Employees[index].ID == id {
78-
return index
79-
}
80-
return -1
81-
}
82-
8365
func main() {
8466
manager := Manager{}
8567
manager.AddEmployee(Employee{ID: 1, Name: "Alice", Age: 30, Salary: 70000})

challenge-3/submissions/shahzodshafizod/solution-template_test.go

Lines changed: 0 additions & 99 deletions
This file was deleted.

challenge-4/submissions/shahzodshafizod/solution-template.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import (
1313
// Return a map from the query (starting node) to the BFS order as a slice of nodes.
1414
// YOU MUST use concurrency (goroutines + channels) to pass the performance tests.
1515
func ConcurrentBFSQueries(graph map[int][]int, queries []int, numWorkers int) map[int][]int {
16-
// TODO: Implement concurrency-based BFS for multiple queries.
17-
// Return an empty map so the code compiles but fails tests if unchanged.
1816
if numWorkers <= 0 {
1917
return map[int][]int{}
2018
}

0 commit comments

Comments
 (0)