Skip to content

Conversation

@baindraraj
Copy link
Contributor

Challenge 3 Solution

Submitted by: @baindraraj
Challenge: Challenge 3

Description

This PR contains my solution for Challenge 3.

Changes

  • Added solution file to challenge-3/submissions/baindraraj/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 Oct 21, 2025

Walkthrough

A new Go program is introduced that implements an Employee struct and a Manager struct with methods to add employees, remove employees by ID, calculate average salary, and find employees by ID, including a main function demonstrating these operations.

Changes

Cohort / File(s) Summary
Employee and Manager implementation
challenge-3/submissions/baindraraj/solution-template.go
Adds Employee struct with ID, Name, Age, Salary fields; Manager struct with Employees slice; four Manager methods (AddEmployee, RemoveEmployee, GetAverageSalary, FindEmployeeByID); main function demonstrating employee management operations

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

The changes introduce straightforward Go types and basic methods with simple logic (appending, iteration, filtering). The code is self-contained in a single file with no complex control flow or business logic.

Possibly related PRs

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The title "Add solution for Challenge 3 by baindraraj" directly and clearly relates to the main change in the pull request. The changeset adds a complete Go program solution file for Challenge 3, and the title accurately summarizes this primary contribution. The title is concise, specific, and provides sufficient context for a reviewer scanning the git history to understand this is a solution submission for Challenge 3, making it an effective and descriptive title.
Description Check ✅ Passed The pull request description is clearly related to the changeset and appropriately describes the submission. It explains that this is a Challenge 3 solution, identifies the file added, and includes relevant testing and code quality information. The description is well-structured, not vague or generic, and directly addresses what was changed in the pull request without being off-topic or misleading.

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

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 464b940 and a7702b7.

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

5-10: LGTM!

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


12-14: LGTM!

The Manager struct is appropriately defined to hold a collection of employees.


61-73: LGTM with dependency on bug fix.

The main function appropriately demonstrates the usage of the Manager and its methods. Once the critical bug in FindEmployeeByID is fixed, this demonstration will work correctly.

Comment on lines +16 to +20
// 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)
}
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 TODO comment.

The method is implemented correctly, but the TODO comment on Line 18 should be removed.

-	// TODO: Implement this method
 	m.Employees = append(m.Employees, e)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// 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)
}
// AddEmployee adds a new employee to the manager's list.
func (m *Manager) AddEmployee(e Employee) {
m.Employees = append(m.Employees, e)
}
🤖 Prompt for AI Agents
In challenge-3/submissions/baindraraj/solution-template.go around lines 16 to
20, remove the leftover TODO comment on line 18 since the AddEmployee method is
implemented correctly; simply delete the "// TODO: Implement this method" line
so the function contains only the implementation and any needed comments
describing behavior.

Comment on lines +22 to +31
// RemoveEmployee removes an employee by ID from the manager's list.
func (m *Manager) RemoveEmployee(id int) {
// TODO: Implement this method
for indx, e := range m.Employees {
if e.ID == id {
m.Employees = append(m.Employees[:indx], m.Employees[indx+1:]...)
break
}
}
}
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 TODO comment.

The method is implemented correctly, but the TODO comment on Line 24 should be removed.

-	// TODO: Implement this method
 	for indx, e := range m.Employees {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// RemoveEmployee removes an employee by ID from the manager's list.
func (m *Manager) RemoveEmployee(id int) {
// TODO: Implement this method
for indx, e := range m.Employees {
if e.ID == id {
m.Employees = append(m.Employees[:indx], m.Employees[indx+1:]...)
break
}
}
}
// RemoveEmployee removes an employee by ID from the manager's list.
func (m *Manager) RemoveEmployee(id int) {
for indx, e := range m.Employees {
if e.ID == id {
m.Employees = append(m.Employees[:indx], m.Employees[indx+1:]...)
break
}
}
}
🤖 Prompt for AI Agents
In challenge-3/submissions/baindraraj/solution-template.go around lines 22 to
31, remove the TODO comment on line 24 because the RemoveEmployee method is
already implemented; delete the "// TODO: Implement this method" line so the
comment no longer incorrectly indicates incomplete work and leave the
implemented loop and logic intact.

Comment on lines +33 to +48
// GetAverageSalary calculates the average salary of all employees.
func (m *Manager) GetAverageSalary() float64 {
// TODO: Implement this method
if len(m.Employees) == 0 {
return 0
}

var total float64
cnt := 0
for _, e := range m.Employees {
total += e.Salary
cnt++
}

return total/ float64(cnt)
}
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 TODO comment and simplify logic.

The method works correctly but has a few minor issues:

  • Line 35: TODO comment should be removed
  • Line 47: Missing space before / operator
  • Lines 41-44: The cnt variable is redundant since len(m.Employees) is already known
-	// TODO: Implement this method
 	if len(m.Employees) == 0 {
 	    return 0
 	}
 	
 	var total float64
-	cnt := 0
 	for _, e := range m.Employees {
 	    total += e.Salary
-	    cnt++
 	}
 	
-	return total/ float64(cnt)
+	return total / float64(len(m.Employees))
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// GetAverageSalary calculates the average salary of all employees.
func (m *Manager) GetAverageSalary() float64 {
// TODO: Implement this method
if len(m.Employees) == 0 {
return 0
}
var total float64
cnt := 0
for _, e := range m.Employees {
total += e.Salary
cnt++
}
return total/ float64(cnt)
}
// GetAverageSalary calculates the average salary of all employees.
func (m *Manager) GetAverageSalary() float64 {
if len(m.Employees) == 0 {
return 0
}
var total float64
for _, e := range m.Employees {
total += e.Salary
}
return total / float64(len(m.Employees))
}
🤖 Prompt for AI Agents
In challenge-3/submissions/baindraraj/solution-template.go around lines 33 to
48, remove the TODO comment, eliminate the redundant cnt variable and use
len(m.Employees) directly, and fix spacing by adding a space before the division
operator so the return becomes total / float64(len(m.Employees)); keep the
empty-slice check returning 0 as-is.

Comment on lines +50 to +59
// FindEmployeeByID finds and returns an employee by their ID.
func (m *Manager) FindEmployeeByID(id int) *Employee {
// TODO: Implement this method
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 bug: returning address of loop variable.

Line 55 returns &e, where e is the range loop variable. In Go, the loop variable is reused in each iteration, so its address remains constant. This means &e will point to the memory location of the loop variable, not the actual employee in the slice. After the loop, this address may contain the last iterated value or be in an undefined state.

-	// TODO: Implement this method
-	for _, e := range m.Employees {
+	for i := range m.Employees {
+	    e := &m.Employees[i]
 	    if e.ID == id {
-	        return &e
+	        return e
 	    }
 	}
 	return nil
🤖 Prompt for AI Agents
In challenge-3/submissions/baindraraj/solution-template.go around lines 50 to
59, the method currently returns &e where e is the range loop variable — fix by
returning the address of the actual slice element instead of the loop variable;
iterate with index (for i := range m.Employees) or use for i, e := range
m.Employees and return &m.Employees[i] when IDs match so the pointer refers to
the real Employee in the slice.

@RezaSi RezaSi merged commit b5eab71 into RezaSi:main Oct 23, 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