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
73 changes: 73 additions & 0 deletions challenge-3/submissions/yz4230/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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) {
idx := -1
for i, e := range m.Employees {
if e.ID == id {
idx = i
break
}
}
if idx == -1 {
return
}
next := make([]Employee, 0, len(m.Employees)-1)
next = append(next, m.Employees[:idx]...)
next = append(next, m.Employees[idx+1:]...)
m.Employees = next
}

// GetAverageSalary calculates the average salary of all employees.
func (m *Manager) GetAverageSalary() float64 {
if len(m.Employees) == 0 {
return 0.0
}
sum := 0.0
for _, e := range m.Employees {
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 i := range m.Employees {
if m.Employees[i].ID == id {
return &m.Employees[i]
}
}
return nil
}
Comment on lines +51 to +59
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 | 🟠 Major

Returning pointer to slice element creates potential dangling pointer issue.

Returning a pointer to a slice element (&m.Employees[i]) is risky because:

  • If the slice is later modified (e.g., via RemoveEmployee or if AddEmployee triggers reallocation), the returned pointer may become invalid or point to incorrect data.
  • This is a common Go pitfall that can lead to subtle, hard-to-debug issues.

Consider returning by value or making a copy instead.

Apply this diff to return by value with a found indicator:

-// FindEmployeeByID finds and returns an employee by their ID.
-func (m *Manager) FindEmployeeByID(id int) *Employee {
+// FindEmployeeByID finds and returns an employee by their ID.
+// Returns the employee and true if found, zero value and false otherwise.
+func (m *Manager) FindEmployeeByID(id int) (Employee, bool) {
 	for i := range m.Employees {
-	    if m.Employees[i].ID == id {
-	        return &m.Employees[i]
-	    }
+		if m.Employees[i].ID == id {
+			return m.Employees[i], true
+		}
 	}
-	return nil
+	return Employee{}, false
 }

Then update the caller in main:

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

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In challenge-3/submissions/yz4230/solution-template.go around lines 51 to 59,
the function currently returns a pointer to a slice element which can become
invalid if the slice is modified; change the function to return the Employee by
value plus a boolean found flag (e.g., (Employee, bool)), iterate to find the
match and return a copy of the employee and true, and return the zero-value
Employee and false when not found; then update all call sites (e.g., in main) to
handle the two-value return, check the boolean before using the Employee, and
avoid storing or relying on pointers to slice elements.


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