Skip to content

Add solution for Challenge 27 by hvijaycse#1451

Merged
RezaSi merged 2 commits intoRezaSi:mainfrom
hvijaycse:challenge-27-hvijaycse
Mar 16, 2026
Merged

Add solution for Challenge 27 by hvijaycse#1451
RezaSi merged 2 commits intoRezaSi:mainfrom
hvijaycse:challenge-27-hvijaycse

Conversation

@hvijaycse
Copy link
Contributor

Challenge 27 Solution

Submitted by: @hvijaycse
Challenge: Challenge 27

Description

This PR contains my solution for Challenge 27.

Changes

  • Added solution file to challenge-27/submissions/hvijaycse/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 Feb 26, 2026

Walkthrough

Adds a single Go file implementing a generics library: Pair, Stack, Queue, Set (mutex-protected), ErrEmptyCollection, and slice utilities (Filter, Map, Reduce, Contains, FindIndex, RemoveDuplicates), plus set algebra functions (Union, Intersection, Difference).

Changes

Cohort / File(s) Summary
Generics library
challenge-27/submissions/hvijaycse/solution-template.go
Adds exported generics: Pair[T,U] (NewPair, Swap); Stack[T], Queue[T] with constructors and empty errors; Set[T comparable] with map+sync.Mutex, Add/Remove/Contains/Elements/Size and Union/Intersection/Difference; ErrEmptyCollection error; slice utilities: Filter, Map, Reduce, Contains, FindIndex, RemoveDuplicates.

Sequence Diagram(s)

(omitted)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐇 I hopped through code with tiny paws,

Stacked my carrots, queued my jaws.
Sets locked safe with mutex might,
Maps and filters in the moonlight.
Generic hops — a rabbit's delight!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a solution file for Challenge 27 submitted by hvijaycse.
Description check ✅ Passed The description is directly related to the changeset, explaining the solution submission and listing the specific file added.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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 (7)
challenge-27/submissions/hvijaycse/solution-template.go (7)

52-63: Remove leftover TODO comment.

The Pop method is fully implemented, but the TODO comment on line 53 remains. This should be cleaned up.

🧹 Proposed fix
 // Pop removes and returns the top element from the stack
 // Returns an error if the stack is empty
 func (s *Stack[T]) Pop() (T, error) {
-	// TODO: Implement this method
 	var zero T
 
 	if s.IsEmpty() {

113-122: Potential memory leak in slice-based queue.

Reslicing with q.Items = q.Items[1:] doesn't release the reference to the dequeued element in the underlying array. For queues holding pointers or large structs, this can cause memory to accumulate.

♻️ Proposed fix to clear the reference
 func (q *Queue[T]) Dequeue() (T, error) {
 	var zero T
 	if q.IsEmpty() {
 		return zero, ErrEmptyCollection
 	}
 
-	zero = q.Items[0]
-	q.Items = q.Items[1:]
-	return zero, nil
+	result := q.Items[0]
+	var empty T
+	q.Items[0] = empty // Clear reference to allow GC
+	q.Items = q.Items[1:]
+	return result, nil
 }

141-145: Remove leftover TODO comment.

The IsEmpty method is implemented, but the TODO comment remains.

🧹 Proposed fix
 // IsEmpty returns true if the queue contains no elements
 func (q *Queue[T]) IsEmpty() bool {
-	// TODO: Implement this method
 	return q.Size() == 0
 }

212-250: Consider idiomatic Go naming for local variables.

Go convention uses camelCase for local variables, not snake_case. Names like nw_set, intr_set, and diff_set could be newSet, intersectSet (or just result), and diffSet.

♻️ Proposed fix
 func Union[T comparable](s1, s2 *Set[T]) *Set[T] {
-
-	nw_set := NewSet[T]()
+	result := NewSet[T]()
 
 	for _, val := range s1.Elements() {
-		nw_set.Add(val)
+		result.Add(val)
 	}
 	for _, val := range s2.Elements() {
-		nw_set.Add(val)
+		result.Add(val)
 	}
-	return nw_set
+	return result
 }
 
 // Intersection returns a new set containing only elements that exist in both sets
 func Intersection[T comparable](s1, s2 *Set[T]) *Set[T] {
-	intr_set := NewSet[T]()
+	result := NewSet[T]()
 	for _, val := range s1.Elements() {
 		if !s2.Contains(val) {
 			continue
 		}
-
-		intr_set.Add(val)
+		result.Add(val)
 	}
-	return intr_set
+	return result
 }
 
 // Difference returns a new set with elements in s1 that are not in s2
 func Difference[T comparable](s1, s2 *Set[T]) *Set[T] {
-	diff_set := NewSet[T]()
+	result := NewSet[T]()
 	for _, val := range s1.Elements() {
 		if s2.Contains(val) {
 			continue
 		}
-
-		diff_set.Add(val)
+		result.Add(val)
 	}
-	return diff_set
+	return result
 }

157-163: Remove leftover TODO comment.

The NewSet function is implemented, but the TODO comment remains.

🧹 Proposed fix
 // NewSet creates a new empty set
 func NewSet[T comparable]() *Set[T] {
-	// TODO: Implement this function
 	return &Set[T]{
 		ItemSet: map[T]struct{}{},
 	}
 }

165-171: Remove leftover TODO comment.

🧹 Proposed fix
 // Add adds an element to the set if it's not already present
 func (s *Set[T]) Add(value T) {
-	// TODO: Implement this method
 	s.mu.Lock()
 	defer s.mu.Unlock()
 	s.ItemSet[value] = struct{}{}
 }

256-325: LGTM!

Utility functions are correctly implemented. Minor nit: mapped_vals (line 271) uses snake_case; Go convention prefers mappedVals.


ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a7ac764 and eb9f7e6.

📒 Files selected for processing (1)
  • challenge-27/submissions/hvijaycse/solution-template.go

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.

🧹 Nitpick comments (2)
challenge-27/submissions/hvijaycse/solution-template.go (2)

52-62: Consider clearer variable naming in Pop and Peek.

The variable zero is reused to hold the actual return value, which is confusing. Consider using distinct names for the zero value and the result.

♻️ Suggested improvement
 func (s *Stack[T]) Pop() (T, error) {
 	var zero T
 
 	if s.IsEmpty() {
 		return zero, ErrEmptyCollection
 	}
 	length := len(s.Items)
-	zero = s.Items[length-1]
+	value := s.Items[length-1]
 	s.Items = s.Items[:length-1]
-	return zero, nil
+	return value, nil
 }

206-243: Consider Go naming conventions for local variables.

Go idiom uses camelCase for local variables. Variables like nw_set, intr_set, and diff_set would be more idiomatic as newSet, intrSet/result, and diffSet/result.

♻️ Example for Union
 func Union[T comparable](s1, s2 *Set[T]) *Set[T] {
-	nw_set := NewSet[T]()
+	result := NewSet[T]()
 
 	for _, val := range s1.Elements() {
-		nw_set.Add(val)
+		result.Add(val)
 	}
 	for _, val := range s2.Elements() {
-		nw_set.Add(val)
+		result.Add(val)
 	}
-	return nw_set
+	return result
 }

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between eb9f7e6 and 423d35e.

📒 Files selected for processing (1)
  • challenge-27/submissions/hvijaycse/solution-template.go

@RezaSi RezaSi merged commit 817fd26 into RezaSi:main Mar 16, 2026
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