Skip to content

Conversation

@kiramux
Copy link
Contributor

@kiramux kiramux commented Nov 4, 2025

Challenge 3 Solution

Submitted by: @kiramux
Challenge: Challenge 3

Description

This PR contains my solution for Challenge 3.

Changes

  • Added solution file to challenge-3/submissions/kiramux/solution-template.go

Testing

  • Solution passes all test cases
  • Code follows Go best practices

Thank you for reviewing my submission! 🚀

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 4, 2025

Walkthrough

A new Go file introduces an Employee management system with Employee and Manager types. The Manager provides methods to add, remove, query employees by ID, and compute average salary, demonstrated in a main function.

Changes

Cohort / File(s) Summary
New Employee Management System
challenge-3/submissions/kiramux/solution-template.go
Adds Employee struct (ID, Name, Age, Salary) and Manager struct with Employees slice. Implements AddEmployee, RemoveEmployee, GetAverageSalary, and FindEmployeeByID methods. Includes main function demonstrating employee operations and output.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Single new file with straightforward data structures and operations
  • All Manager methods implement basic slice operations without complex logic
  • Clear, readable implementation suitable for quick verification

Possibly related PRs

Pre-merge checks

✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adding a solution for Challenge 3 by a specific contributor, which matches the file addition and PR objectives.
Description check ✅ Passed The description is directly related to the changeset, clearly stating this is a Challenge 3 solution submission with the specific file path and testing confirmations.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
challenge-3/submissions/kiramux/solution-template.go (1)

40-53: Consider using range loop for more idiomatic Go.

The implementation is correct. However, using a range loop would be more idiomatic.

Apply this diff:

-	var sum float64
-	for i := 0; i < size; i++ {
-	    sum += m.Employees[i].Salary
-	}
-	avg := sum / float64(size)
-	return avg
+	var sum float64
+	for _, emp := range m.Employees {
+	    sum += emp.Salary
+	}
+	return sum / float64(size)
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 231bd24 and 1e47a80.

📒 Files selected for processing (1)
  • challenge-3/submissions/kiramux/solution-template.go (1 hunks)
🔇 Additional comments (1)
challenge-3/submissions/kiramux/solution-template.go (1)

17-20: LGTM!

The implementation correctly appends the new employee to the manager's list.

Comment on lines +23 to +37
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]
}
}
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.

Comment on lines +56 to +65
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
}
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.

@RezaSi RezaSi merged commit c70e5ad into RezaSi:main Nov 6, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants