Skip to content

Conversation

@AlexO-85
Copy link
Contributor

@AlexO-85 AlexO-85 commented Nov 5, 2025

Challenge 3 Solution

Submitted by: @AlexO-85
Challenge: Challenge 3

Description

This PR contains my solution for Challenge 3.

Changes

  • Added solution file to challenge-3/submissions/AlexO-85/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 5, 2025

Walkthrough

This PR introduces a Go solution for Challenge 3 that implements Employee and Manager types with methods for managing employee records. The Employee struct stores employee data, while Manager maintains a slice of employees and provides operations to add, remove, query, and compute average salary. A main function demonstrates the complete workflow.

Changes

Cohort / File(s) Change Summary
Challenge 3 Employee Management Solution
challenge-3/submissions/AlexO-85/solution-template.go
Added Employee struct (ID, Name, Age, Salary), Manager struct with Employees slice, and Manager methods: AddEmployee (appends), RemoveEmployee (removes first match by ID), GetAverageSalary (returns mean or 0), FindEmployeeByID (returns pointer or nil). Includes main function demonstrating usage with employee creation, removal, average calculation, and lookup operations.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

  • Simple struct definitions with basic field types
  • Straightforward method implementations using standard slice operations
  • No complex control flow or error handling logic
  • Clear, procedural main function for demonstration

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 (AlexO-85).
Description check ✅ Passed The description is directly related to the changeset, providing context about the Challenge 3 solution submission, the file location, and testing status.

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: 1

🧹 Nitpick comments (2)
challenge-3/submissions/AlexO-85/solution-template.go (2)

21-29: Remove unnecessary parentheses and semicolon.

The logic is correct, but the code includes Go style deviations:

  • Line 24: Parentheses around the if condition are unnecessary and not idiomatic in Go.
  • Line 26: The semicolon after break is unnecessary and not idiomatic in Go.

Apply this diff for idiomatic Go style:

-	    if(e.ID == id){
+	    if e.ID == id {
 	        m.Employees = append(m.Employees[:i], m.Employees[i+1:]...)
-	        break;
+	        break
 	    }

31-48: Use pre-calculated length and remove unnecessary parentheses.

The logic is correct, but there are style improvements:

  • Line 37: Unnecessary parentheses around the if condition.
  • Line 45: Recalculates len(m.Employees) instead of using the countEmployees variable from Line 34.

Apply this diff for consistency:

-    if(countEmployees == 0){
+    if countEmployees == 0 {
         return sumSalary
     }
     
 	for _, e := range m.Employees {
 	    sumSalary += e.Salary
 	}
 	
-	avgSalary := sumSalary / float64(len(m.Employees))
+	avgSalary := sumSalary / float64(countEmployees)
 	
 	return avgSalary
📜 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 4f3fedb.

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

5-10: LGTM!

The Employee struct is well-defined with appropriate field types and naming.


12-14: LGTM!

The Manager struct is appropriately defined.


16-19: LGTM!

The AddEmployee method correctly uses a pointer receiver and appends to the slice idiomatically.


60-72: LGTM!

The main function provides a good demonstration of the Manager API with appropriate test cases and output formatting.

Comment on lines +50 to +58
// FindEmployeeByID finds and returns an employee by their ID.
func (m *Manager) FindEmployeeByID(id int) *Employee {
for _, e := range m.Employees {
if(e.ID == id){
return &e
}
}
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: Fix loop variable address capture bug.

Line 54 contains a critical bug: returning &e captures the address of the loop variable, not the actual employee in the slice. In Go, the loop variable e is reused for each iteration, so its address remains the same and points to a temporary copy. This will lead to incorrect or unpredictable behavior.

Additionally, Line 53 has unnecessary parentheses around the if condition.

Apply this diff to fix both issues:

 func (m *Manager) FindEmployeeByID(id int) *Employee {
-	for _, e := range m.Employees {
-	    if(e.ID == id){
-	        return &e
+	for i := range m.Employees {
+	    if m.Employees[i].ID == id {
+	        return &m.Employees[i]
 	    }
 	}
 	return nil
🤖 Prompt for AI Agents
In challenge-3/submissions/AlexO-85/solution-template.go around lines 50 to 58,
the function returns &e which captures the loop variable's address and also
contains unnecessary parentheses in the if; to fix, iterate with index (for i :=
range m.Employees) or use for i, e := range m.Employees and return
&m.Employees[i] instead of &e, and remove the extraneous parentheses around the
if condition so it reads if e.ID == id { ... }.

@RezaSi RezaSi merged commit 2b60cbb 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