Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions challenge-3/submissions/nzamulov/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import "fmt"

type Employee struct {
ID int
Name string
Age int
Salary float64
}

type Manager struct {
Employees []Employee
}

// AddEmployee adds a new employee to the manager's list.
func (m *Manager) AddEmployee(e Employee) {
m.Employees = append(m.Employees, e)
}

// RemoveEmployee removes an employee by ID from the manager's list.
func (m *Manager) RemoveEmployee(id int) {
for i, e := range m.Employees {
if e.ID == id {
m.Employees = append(m.Employees[:i], m.Employees[i+1:]...)
break
}
}
}

// GetAverageSalary calculates the average salary of all employees.
func (m *Manager) GetAverageSalary() float64 {
if len(m.Employees) == 0 {
return 0
}
var total float64 = 0
for _, e := range m.Employees {
total += e.Salary
}
return total / float64(len(m.Employees))
}

// FindEmployeeByID finds and returns an employee by their ID.
func (m *Manager) FindEmployeeByID(id int) *Employee {
for _, e := range m.Employees {
if e.ID == id {
return &e
}
}
return nil
}
Comment on lines +43 to +51
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical bug: returning address of loop variable.

Line 47 returns &e where e is the range loop variable. In Go, the loop variable is reused across iterations, so this returns the address of a temporary copy rather than the actual slice element. This pointer may become invalid or point to incorrect data after the loop.

Apply this diff to fix the issue by returning the address of the slice element:

 func (m *Manager) FindEmployeeByID(id int) *Employee {
-	for _, e := range m.Employees {
+	for i, e := range m.Employees {
 	    if e.ID == id {
-	        return &e
+	        return &m.Employees[i]
 	    }
 	}
 	return nil
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// FindEmployeeByID finds and returns an employee by their ID.
func (m *Manager) FindEmployeeByID(id int) *Employee {
for _, e := range m.Employees {
if e.ID == id {
return &e
}
}
return nil
}
// FindEmployeeByID finds and returns an employee by their ID.
func (m *Manager) FindEmployeeByID(id int) *Employee {
for i, e := range m.Employees {
if e.ID == id {
return &m.Employees[i]
}
}
return nil
}
🤖 Prompt for AI Agents
In challenge-3/submissions/nzamulov/solution-template.go around lines 43 to 51,
the function FindEmployeeByID returns &e (address of the range loop variable)
which is a reused temporary; replace that with a pointer to the actual slice
element (for example iterate with index and return &m.Employees[i] or capture
the element by index) so the returned pointer references the real slice element
rather than the loop variable.


func main() {
manager := Manager{}
manager.AddEmployee(Employee{ID: 1, Name: "Alice", Age: 30, Salary: 70000})
manager.AddEmployee(Employee{ID: 2, Name: "Bob", Age: 25, Salary: 65000})
manager.RemoveEmployee(1)
averageSalary := manager.GetAverageSalary()
employee := manager.FindEmployeeByID(2)

fmt.Printf("Average Salary: %f\n", averageSalary)
if employee != nil {
fmt.Printf("Employee found: %+v\n", *employee)
}
}
Loading