Skip to content

Conversation

@nzamulov
Copy link
Contributor

@nzamulov nzamulov commented Nov 6, 2025

Challenge 3 Solution

Submitted by: @nzamulov
Challenge: Challenge 3

Description

This PR contains my solution for Challenge 3.

Changes

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

Walkthrough

A new Go program is added implementing Employee and Manager types with methods to manage employee collections: AddEmployee, RemoveEmployee, GetAverageSalary, and FindEmployeeByID. The main function demonstrates creating a manager, removing an employee, calculating average salary, and locating an employee by ID.

Changes

Cohort / File(s) Summary
New Employee Manager Implementation
challenge-3/submissions/nzamulov/solution-template.go
Adds Employee and Manager types with methods for managing employees: AddEmployee appends to the list, RemoveEmployee filters by ID, GetAverageSalary computes mean salary, FindEmployeeByID searches by ID. Main demonstrates the workflow with employee creation, removal, and querying.

Sequence Diagram

sequenceDiagram
    participant Main
    participant Manager
    participant EmployeeList

    Main->>Manager: Create Manager
    Main->>Manager: AddEmployee(Employee{ID:1, Name:"Alice", Salary:50000})
    Manager->>EmployeeList: Append
    Main->>Manager: AddEmployee(Employee{ID:2, Name:"Bob", Salary:60000})
    Manager->>EmployeeList: Append
    Main->>Manager: RemoveEmployee(1)
    Manager->>EmployeeList: Filter out ID 1
    Main->>Manager: GetAverageSalary()
    Manager->>EmployeeList: Calculate mean
    Manager-->>Main: Return average
    Main->>Manager: FindEmployeeByID(2)
    Manager->>EmployeeList: Search by ID
    Manager-->>Main: Return employee pointer
    Main->>Main: Print results
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Areas requiring extra attention:

  • FindEmployeeByID bug: The method returns the address of a loop variable (&emp), which creates a pointer to a copy rather than the actual slice element. This is a critical bug that will cause undefined behavior when accessing the pointer after the loop completes.
  • Correctness of RemoveEmployee: Verify the slicing logic correctly removes the target employee and preserves list integrity.
  • Edge cases: Confirm behavior with empty employee lists and non-existent IDs.

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 identifies that this PR adds a solution submission for Challenge 3 by the user nzamulov, which directly matches the changeset content.
Description check ✅ Passed The description is directly related to the changeset, explaining the Challenge 3 solution submission with specific file path and indicating test validation.

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/nzamulov/solution-template.go (2)

21-29: Consider returning a success indicator.

The removal logic is correct, but the method doesn't indicate whether an employee was found and removed. Consider returning a bool to signal success, which can help callers handle cases where the ID doesn't exist.

Apply this diff to add a return value:

-func (m *Manager) RemoveEmployee(id int) {
+func (m *Manager) RemoveEmployee(id int) bool {
 	for i, e := range m.Employees {
 	    if e.ID == id {
 	        m.Employees = append(m.Employees[:i], m.Employees[i+1:]...)
-	        break
+	        return true
 	    }
 	}
+	return false
 }

31-41: Consider simplifying variable initialization.

The average salary calculation is correct and includes good defensive programming for the empty case. Line 36 could be simplified since the zero value of float64 is already 0.

Apply this diff:

-	var total float64 = 0
+	var total float64
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1a06951 and 7d898cb.

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

5-10: LGTM!

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


12-14: LGTM!

The Manager struct appropriately encapsulates a collection of employees.


16-19: LGTM!

The AddEmployee method correctly uses a pointer receiver and idiomatic append to add employees to the manager's list.


53-65: LGTM!

The main function effectively demonstrates all the Manager methods and includes proper nil checking before dereferencing the employee pointer.

Comment on lines +43 to +51
// 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 bug: returning address of loop variable.

Line 47 returns &e where e is the range loop variable. In Go, the loop variable is reused across iterations, so this returns the address of a temporary copy rather than the actual slice element. This pointer may become invalid or point to incorrect data after the loop.

Apply this diff to fix the issue by returning the address of the slice element:

 func (m *Manager) FindEmployeeByID(id int) *Employee {
-	for _, e := range m.Employees {
+	for i, e := range m.Employees {
 	    if e.ID == id {
-	        return &e
+	        return &m.Employees[i]
 	    }
 	}
 	return nil
 }
📝 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
// 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
}
// FindEmployeeByID finds and returns an employee by their ID.
func (m *Manager) FindEmployeeByID(id int) *Employee {
for i, e := range m.Employees {
if e.ID == id {
return &m.Employees[i]
}
}
return nil
}
🤖 Prompt for AI Agents
In challenge-3/submissions/nzamulov/solution-template.go around lines 43 to 51,
the function FindEmployeeByID returns &e (address of the range loop variable)
which is a reused temporary; replace that with a pointer to the actual slice
element (for example iterate with index and return &m.Employees[i] or capture
the element by index) so the returned pointer references the real slice element
rather than the loop variable.

@nzamulov nzamulov closed this Nov 6, 2025
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.

1 participant