Skip to content
Open
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/kushalShukla-web/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
}

func (m *Manager) GetEmployee() {

}
Comment on lines +15 to +17
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 | 🟡 Minor

Remove or implement the GetEmployee stub.

This method is empty and has no return value. If it's not needed, remove it. Otherwise, implement the intended functionality.


func (m *Manager) AddEmployee(em Employee) {
m.Employees = append(m.Employees, em)
}

func (m *Manager) RemoveEmployee(id int) {
var tempValue []Employee
for i := 0; i < len(m.Employees); i++ {
if m.Employees[i].ID == id {
continue
} else {
tempValue = append(tempValue, m.Employees[i])
}
}
m.Employees = tempValue
}

func (m *Manager) GetAverageSalary() float64 {
if len(m.Employees) == 0 {
return 0
}
var averagesalary float64
for i := 0; i < len(m.Employees); i++ {
averagesalary += m.Employees[i].Salary
}

averagesalary = averagesalary / float64(len(m.Employees))
return averagesalary
}

func (m *Manager) FindEmployeeByID(id int) *Employee {
for i := 0; i < len(m.Employees); i++ {
if m.Employees[i].ID == id {
return &m.Employees[i]
}
}
return nil
}
Comment on lines +48 to +55
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

Potential dangling pointer after slice modifications.

Returning a pointer to a slice element (&m.Employees[i]) becomes unsafe when RemoveEmployee is called, as it rebuilds the slice. The returned pointer may then reference invalid or incorrect data.

Option 1: Return by value instead of pointer

-func (m *Manager) FindEmployeeByID(id int) *Employee {
-	for i := 0; i < len(m.Employees); i++ {
-		if m.Employees[i].ID == id {
-			return &m.Employees[i]
+func (m *Manager) FindEmployeeByID(id int) (Employee, bool) {
+	for _, employee := range m.Employees {
+		if employee.ID == id {
+			return employee, true
 		}
 	}
-	return nil
+	return Employee{}, false
 }

Then update the caller in main:

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

Option 2: Use a map instead of slice for O(1) lookups

This would also eliminate the pointer safety issue and improve performance.

🤖 Prompt for AI Agents
In challenge-3/submissions/kushalShukla-web/solution-template.go around lines
48–55, the function returns a pointer to a slice element (&m.Employees[i]) which
can dangle after slice modifications; change the API to return the Employee by
value (and a found flag) instead of a pointer: update the function signature to
return (Employee, bool), return the matched element by value with true, and
return (Employee{}, false) when not found; then update all callers in main/tests
to handle the value + bool pattern. As an alternative (optional) refactor,
replace the slice with a map[int]Employee for O(1) lookups and safe value
returns.


func main() {
manager := Manager{}
manager.AddEmployee(Employee{ID: 1, Name: "Kushal", Age: 23, Salary: 4500})
manager.AddEmployee(Employee{ID: 2, Name: "Bob", Age: 25, Salary: 65000})
manager.RemoveEmployee(1)
averagesalary := manager.GetAverageSalary()
fmt.Println("The average salary is ", averagesalary)
employee := manager.FindEmployeeByID(2)
if employee != nil {
fmt.Printf("Employee found: %+v\n", *employee)
}
}