Skip to content

Conversation

@iamsurajmandal
Copy link
Contributor

Challenge 3 Solution

Submitted by: @iamsurajmandal
Challenge: Challenge 3

Description

This PR contains my solution for Challenge 3.

Changes

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

Walkthrough

A Go solution file is added implementing an Employee and Manager system. The code defines two public types (Employee and Manager) with four Manager methods for employee lifecycle management (add, remove, find by ID) and salary calculations, plus a main function demonstrating basic usage.

Changes

Cohort / File(s) Summary
Challenge 3 Employee Manager Solution
challenge-3/submissions/iamsurajmandal/solution-template.go
Introduces Employee and Manager types with methods: AddEmployee (appends employee), RemoveEmployee (removes by ID), GetAverageSalary (calculates average), and FindEmployeeByID (searches and returns pointer). Includes main function demonstrating all operations.

Sequence Diagram

sequenceDiagram
    participant Main
    participant Manager
    participant Employees

    Main->>Manager: NewManager()
    Main->>Manager: AddEmployee(Alice, salary: 50000)
    Manager->>Employees: append
    Main->>Manager: AddEmployee(Bob, salary: 60000)
    Manager->>Employees: append
    Main->>Manager: RemoveEmployee(1)
    Manager->>Employees: iterate & remove
    Main->>Manager: GetAverageSalary()
    Manager->>Employees: iterate all
    Manager-->>Main: 60000.0
    Main->>Manager: FindEmployeeByID(2)
    Manager->>Employees: search
    Manager-->>Main: *Employee (Bob)
    Main->>Main: Print results
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Verify RemoveEmployee behavior (currently continues iteration after removal—confirm if intentional or a potential bug)
  • Check nil pointer safety in FindEmployeeByID usage
  • Validate GetAverageSalary division by zero handling (returns 0 for empty list)

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 clearly and specifically summarizes the main change: adding a Challenge 3 solution by iamsurajmandal, which directly matches the changeset.
Description check ✅ Passed The description is directly related to the changeset, explaining that it contains a Challenge 3 solution file with appropriate context about the submission.

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/iamsurajmandal/solution-template.go (1)

38-47: Consider minor style improvements.

The logic is correct, but consider these idiomatic Go refinements:

  • Line 39: 0.000000 can be simplified to 0.0
  • Line 40: Remove unnecessary parentheses around the condition

Apply this diff:

 func (m *Manager) GetAverageSalary() float64 {
-	sum := 0.000000 
-	if (len(m.Employees) < 1) {
+	sum := 0.0
+	if len(m.Employees) == 0 {
 	    return sum
 	}
 	for _, item := range m.Employees {
 	    sum += item.Salary
 	}
 	return sum / float64(len(m.Employees))
 }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2d4b9cf and 1294276.

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

5-14: LGTM!

The struct definitions are clean and follow Go conventions with properly exported fields.


50-57: LGTM!

The implementation correctly returns a pointer to the employee in the slice and properly handles the not-found case by returning nil.


60-72: LGTM!

The main function effectively demonstrates all the Manager methods with clear output.

Comment on lines +17 to +25
func (m *Manager) AddEmployee(e Employee) {
// TODO: Implement this method
m.Employees = append(m.Employees,Employee{
ID: e.ID,
Name: e.Name,
Age: e.Age,
Salary: e.Salary,
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

Simplify: append the parameter directly.

The explicit field-by-field copy is unnecessary. Since e is already passed by value in Go, you can append it directly.

Apply this diff:

 func (m *Manager) AddEmployee(e Employee) {
-	// TODO: Implement this method
-	m.Employees = append(m.Employees,Employee{
-	    ID: e.ID,
-	    Name: e.Name, 
-	    Age: e.Age,
-	    Salary: e.Salary,
-	})
+	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
func (m *Manager) AddEmployee(e Employee) {
// TODO: Implement this method
m.Employees = append(m.Employees,Employee{
ID: e.ID,
Name: e.Name,
Age: e.Age,
Salary: e.Salary,
})
}
func (m *Manager) AddEmployee(e Employee) {
m.Employees = append(m.Employees, e)
}
🤖 Prompt for AI Agents
In challenge-3/submissions/iamsurajmandal/solution-template.go around lines 17
to 25, the AddEmployee method unnecessarily constructs a new Employee literal
copying fields from the parameter; simply append the passed-in parameter
directly to m.Employees (i.e., replace the explicit struct literal with
append(m.Employees, e)) and remove the field-by-field copy.

Comment on lines +28 to +35
func (m *Manager) RemoveEmployee(id int) {
// TODO: Implement this method
for index, item := range m.Employees {
if (item.ID == id) {
m.Employees = append( m.Employees[:index],m.Employees[index + 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 | 🟠 Major

Add break after removal to prevent iteration over modified slice.

After removing an element at line 32, the loop continues iterating over the modified slice. This can lead to skipped elements or unexpected behavior. The standard practice is to break immediately after removal.

Apply this diff:

 func (m *Manager) RemoveEmployee(id int) {
-	// TODO: Implement this method
 	for index, item := range m.Employees {
-	    if (item.ID == id) {
+	    if item.ID == id {
 	        m.Employees = append( m.Employees[:index],m.Employees[index + 1:]...)
+	        break
 	    }
 	}
 }

Note: Also removed unnecessary parentheses around the condition for Go idiomaticity.

📝 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
func (m *Manager) RemoveEmployee(id int) {
// TODO: Implement this method
for index, item := range m.Employees {
if (item.ID == id) {
m.Employees = append( m.Employees[:index],m.Employees[index + 1:]...)
}
}
}
func (m *Manager) RemoveEmployee(id int) {
for index, item := range m.Employees {
if item.ID == id {
m.Employees = append( m.Employees[:index],m.Employees[index + 1:]...)
break
}
}
}
🤖 Prompt for AI Agents
In challenge-3/submissions/iamsurajmandal/solution-template.go around lines 28
to 35 the loop removes an employee from the slice but continues iterating over
the now-modified slice which can skip elements or cause unexpected behavior;
after performing the slice append to remove the element, immediately break out
of the loop to stop further iteration, and also remove the unnecessary
parentheses around the if condition to match Go idioms.

@RezaSi RezaSi merged commit 1efee9c into RezaSi:main Nov 9, 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