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
67 changes: 67 additions & 0 deletions challenge-3/submissions/Sairaviteja27/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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,v:= range m.Employees {
if v.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.0
}

var totalSalary float64
for _, e := range m.Employees {
totalSalary += e.Salary
}

return totalSalary / float64(len(m.Employees))
}

// FindEmployeeByID finds and returns an employee by their ID.
func (m *Manager) FindEmployeeByID(id int) *Employee {
for _,v:= range m.Employees {
if v.ID==id{
return &v
}
}
return nil
}
Comment on lines +45 to +53
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 pointer to loop variable.

Line 49 returns &v, which is the address of the loop variable. In Go, the loop variable is reused for each iteration, so this returns an invalid pointer that will not correctly reference the employee in the slice.

Apply this diff to fix the issue by returning a pointer to the slice element:

-	for _,v:= range m.Employees {
-	    if v.ID==id{
-	       return &v
+	for i, v := range m.Employees {
+	    if v.ID == id {
+	       return &m.Employees[i]
 	    }
 	}

Alternatively, you can iterate by index only:

-	for _,v:= range m.Employees {
-	    if v.ID==id{
-	       return &v
+	for i := range m.Employees {
+	    if m.Employees[i].ID == id {
+	       return &m.Employees[i]
 	    }
 	}
📝 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 _,v:= range m.Employees {
if v.ID==id{
return &v
}
}
return nil
}
// FindEmployeeByID finds and returns an employee by their ID.
func (m *Manager) FindEmployeeByID(id int) *Employee {
for i, v := range m.Employees {
if v.ID == id {
return &m.Employees[i]
}
}
return nil
}
🤖 Prompt for AI Agents
In challenge-3/submissions/Sairaviteja27/solution-template.go around lines 45 to
53, the function returns &v which is the address of the reused loop variable;
change the loop to either iterate by index or return the address of the actual
slice element (e.g., for i := range m.Employees { if m.Employees[i].ID == id {
return &m.Employees[i] } }) so you return a stable pointer to the element; keep
the final return nil unchanged.


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