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
79 changes: 79 additions & 0 deletions challenge-3/submissions/kiramux/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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) {
// TODO: Implement this method
m.Employees = append(m.Employees, e)
}

// RemoveEmployee removes an employee by ID from the manager's list.
func (m *Manager) RemoveEmployee(id int) {
// TODO: Implement this method
size := len(m.Employees)
flag := -1
for i := range m.Employees {
if m.Employees[i].ID == id {
flag = i
break
}
}
if flag != -1 {
m.Employees[flag] = m.Employees[size-1]
m.Employees = m.Employees[:size-1]
}
}
Comment on lines +23 to +37
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

Fix indentation to use tabs.

The code uses spaces for indentation instead of tabs, which violates Go conventions. Run gofmt or go fmt to automatically fix the indentation throughout the file.

Apply gofmt to fix:

#!/bin/bash
# Run gofmt to fix indentation
go fmt challenge-3/submissions/kiramux/solution-template.go
🤖 Prompt for AI Agents
challenge-3/submissions/kiramux/solution-template.go lines 23-37: the function
uses spaces for indentation instead of tabs; run gofmt (or go fmt) on this file
to replace spaces with Go-standard tabs and normalize formatting (e.g., run
`gofmt -w challenge-3/submissions/kiramux/solution-template.go` or `go fmt
./challenge-3/submissions/kiramux`) so the indentation and overall formatting
conform to Go conventions.


// GetAverageSalary calculates the average salary of all employees.
func (m *Manager) GetAverageSalary() float64 {
// TODO: Implement this method
size := len(m.Employees)
if size == 0 {
return 0
}

var sum float64
for i := 0; i < size; i++ {
sum += m.Employees[i].Salary
}
avg := sum / float64(size)
return avg
}

// FindEmployeeByID finds and returns an employee by their ID.
func (m *Manager) FindEmployeeByID(id int) *Employee {
// TODO: Implement this method
for i := range m.Employees {
if m.Employees[i].ID == id {
return &(m.Employees[i])
}
}
fmt.Println("Cannot find employee by ID: ", id)
return nil
}
Comment on lines +56 to +65
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 pointer to slice element is unsafe.

Returning &m.Employees[i] creates a dangling pointer risk. When RemoveEmployee modifies the slice (swap-and-truncate), any existing pointers become stale and may point to wrong data or invalid memory.

Additionally, printing the error message directly (line 63) violates separation of concerns. The caller should decide how to handle a not-found case.

Apply this diff to return a copy instead and remove direct printing:

-func (m *Manager) FindEmployeeByID(id int) *Employee {
+func (m *Manager) FindEmployeeByID(id int) (Employee, bool) {
 	// TODO: Implement this method
 	for i := range m.Employees {
 	    if m.Employees[i].ID == id {
-	        return &(m.Employees[i])
+	        return m.Employees[i], true
 	    } 
 	}
-	fmt.Println("Cannot find employee by ID: ", id)
-	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)
+	} else {
+		fmt.Printf("Employee not found\n")
 	}

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

🤖 Prompt for AI Agents
In challenge-3/submissions/kiramux/solution-template.go around lines 56–65, do
not return a pointer to a slice element or print from inside the method; change
the signature to return a copy and a found flag (e.g., FindEmployeeByID(id int)
(Employee, bool)), iterate to find the employee and return the Employee value
with true, and return the zero-value Employee with false when not found (remove
the fmt.Println). Then update the caller(s) in main to handle the (Employee,
bool) result: check the bool, log or handle the not-found case there, and use
the returned copy when found.


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