Skip to content

Conversation

@Sairaviteja27
Copy link
Contributor

Challenge 3 Solution

Submitted by: @Sairaviteja27
Challenge: Challenge 3

Description

This PR contains my solution for Challenge 3.

Changes

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

Walkthrough

Introduces a Go program implementing employee management functionality with an Employee struct containing ID, Name, Age, and Salary fields, and a Manager struct with methods to add/remove employees, calculate average salary, and search for employees by ID.

Changes

Cohort / File(s) Summary
Employee Manager Implementation
challenge-3/submissions/Sairaviteja27/solution-template.go
Added Employee and Manager types with AddEmployee, RemoveEmployee, GetAverageSalary, and FindEmployeeByID methods. Includes main function demonstrating CRUD operations and querying on a collection of employees.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

The change introduces straightforward data structures and slice-based CRUD operations. Review should focus on validating the slice manipulation logic in RemoveEmployee, verifying the average salary calculation handles edge cases correctly, and identifying the noted pointer-to-loop-variable issue in FindEmployeeByID that could produce invalid memory references after iteration completes.

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 pull request title "Add solution for Challenge 3 by Sairaviteja27" is directly related to the main change in the changeset, which introduces a Go solution file for Challenge 3 at the specified path. The title is concise, clear, and specific enough that someone reviewing the repository history would understand the primary change is adding a challenge solution submission. The title accurately captures the essence of the modification without unnecessary verbosity or irrelevant details.
Description Check ✅ Passed The pull request description is clearly related to the changeset and provides relevant information about the submission. It specifies that this is a Challenge 3 solution by Sairaviteja27, indicates the file location, and mentions testing and code quality considerations. The description contains sufficient detail to convey the purpose and scope of the changes without being overly vague or generic, making it appropriate for a challenge submission context.

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

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 12f287c and 2fa9f21.

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

16-19: LGTM!

The method correctly appends the employee to the manager's list using the pointer receiver.


21-29: LGTM!

The method correctly removes the first employee with the matching ID using slice manipulation and breaks appropriately.


31-43: LGTM!

The method correctly calculates the average salary with proper handling of the empty slice case.


55-67: Good demonstration of the API.

The main function correctly demonstrates all the manager methods with proper nil checking before dereferencing the employee pointer.

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

Line 49 returns &v, which is the address of the loop variable. In Go, the loop variable is reused for each iteration, so this returns an invalid pointer that will not correctly reference the employee in the slice.

Apply this diff to fix the issue by returning a pointer to the slice element:

-	for _,v:= range m.Employees {
-	    if v.ID==id{
-	       return &v
+	for i, v := range m.Employees {
+	    if v.ID == id {
+	       return &m.Employees[i]
 	    }
 	}

Alternatively, you can iterate by index only:

-	for _,v:= range m.Employees {
-	    if v.ID==id{
-	       return &v
+	for i := range m.Employees {
+	    if m.Employees[i].ID == id {
+	       return &m.Employees[i]
 	    }
 	}
📝 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 _,v:= range m.Employees {
if v.ID==id{
return &v
}
}
return nil
}
// FindEmployeeByID finds and returns an employee by their ID.
func (m *Manager) FindEmployeeByID(id int) *Employee {
for i, v := range m.Employees {
if v.ID == id {
return &m.Employees[i]
}
}
return nil
}
🤖 Prompt for AI Agents
In challenge-3/submissions/Sairaviteja27/solution-template.go around lines 45 to
53, the function returns &v which is the address of the reused loop variable;
change the loop to either iterate by index or return the address of the actual
slice element (e.g., for i := range m.Employees { if m.Employees[i].ID == id {
return &m.Employees[i] } }) so you return a stable pointer to the element; keep
the final return nil unchanged.

@RezaSi RezaSi merged commit 05f6d22 into RezaSi:main Oct 20, 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