Skip to content
Merged
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
68 changes: 68 additions & 0 deletions challenge-3/submissions/ansmonjol/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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 index, value := range m.Employees {
if id == value.ID {
m.Employees = append(m.Employees[:index], m.Employees[index+1:]...)
}
}
}
Comment on lines +21 to +28
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: Missing break/return after slice modification during iteration.

Modifying a slice during a range loop without breaking is unsafe. After removing an element at index, the slice is shortened and subsequent elements shift left, but the range loop continues with stale iteration state. This can skip elements or cause unexpected behavior.

Apply this diff to fix the issue by adding a return statement:

 func (m *Manager) RemoveEmployee(id int) {
 	for index, value := range m.Employees {
 		if id == value.ID {
 			m.Employees = append(m.Employees[:index], m.Employees[index+1:]...)
+			return
 		}
 	}
 }

Alternatively, if multiple employees might share the same ID, iterate backwards:

func (m *Manager) RemoveEmployee(id int) {
	for i := len(m.Employees) - 1; i >= 0; i-- {
		if m.Employees[i].ID == id {
			m.Employees = append(m.Employees[:i], m.Employees[i+1:]...)
		}
	}
}
🤖 Prompt for AI Agents
In challenge-3/submissions/ansmonjol/solution-template.go around lines 21 to 28,
the RemoveEmployee method modifies the Employees slice inside a range loop which
can skip elements or produce incorrect behavior; fix it by either returning
immediately after the append removal so the loop stops after the first match, or
if duplicate IDs must be removed, rewrite the loop to iterate backwards from
len(m.Employees)-1 down to 0 and perform the append removal for each matching
index.


// GetAverageSalary calculates the average salary of all employees.
func (m *Manager) GetAverageSalary() float64 {
sum := 0.0

if len(m.Employees) == 0 {
return sum
}

for _, e := range m.Employees {
sum = sum + e.Salary
}

return sum / 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 +45 to +54
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: Returning address of loop variable.

Returning &e is unsafe because e is the loop variable that is reused for each iteration. The address remains the same across all iterations, so you're returning a pointer to a temporary that may contain stale or incorrect data after the loop exits.

Apply this diff to fix by using an indexed loop:

 func (m *Manager) FindEmployeeByID(id int) *Employee {
-	for _, e := range m.Employees {
-		if e.ID == id {
-			return &e
+	for i := range m.Employees {
+		if m.Employees[i].ID == id {
+			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 := range m.Employees {
if m.Employees[i].ID == id {
return &m.Employees[i]
}
}
return nil
}
🤖 Prompt for AI Agents
In challenge-3/submissions/ansmonjol/solution-template.go around lines 45 to 54,
the function returns the address of the loop variable `e`, which is reused
across iterations and leads to incorrect/stale pointers; change to an indexed
loop over m.Employees and return the address of the slice element (e.g.,
&m.Employees[i]) when IDs match so you return a stable pointer to the actual
Employee stored in the slice.


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