Skip to content

Add solution for Challenge 30 by imankhodadi#1080

Closed
imankhodadi wants to merge 1 commit intoRezaSi:mainfrom
imankhodadi:challenge-30-imankhodadi-1767718802
Closed

Add solution for Challenge 30 by imankhodadi#1080
imankhodadi wants to merge 1 commit intoRezaSi:mainfrom
imankhodadi:challenge-30-imankhodadi-1767718802

Conversation

@imankhodadi
Copy link
Contributor

Challenge 30 Solution

Submitted by: @imankhodadi
Challenge: Challenge 30

Description

This PR contains my solution for Challenge 30.

Changes

  • Added solution file to challenge-30/submissions/imankhodadi/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 Jan 6, 2026

Walkthrough

Introduces a new Go context management module with a ContextManager interface providing comprehensive context manipulation capabilities, including cancellation, timeout handling, value attachment, task execution with cancellation awareness, concurrent processing, operation chaining, rate-limited execution, and cleanup support.

Changes

Cohort / File(s) Summary
New Context Management Module
challenge-30/submissions/imankhodadi/solution-template.go
Introduces ContextManager interface with 14 methods for context lifecycle management (create cancellable/timeout contexts, attach/retrieve values), task execution patterns (execute with context/timeout, execute with cleanup, chain operations, rate-limited execution), synchronization utilities (wait for completion with/without progress callbacks), and concurrent item processing with worker pools. Helper functions provide work simulation with progress tracking and cancellation-aware processing.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant CM as ContextManager
    participant Task
    participant Cancel as Cancellation Channel

    rect rgb(220, 240, 255)
    Note over Client,Cancel: Task Execution with Cancellation
    Client->>CM: ExecuteWithContext(ctx, task)
    activate CM
    
    rect rgb(240, 248, 255)
    Note over CM,Task: Check context before execution
    CM->>Cancel: Select ctx.Done()
    end
    
    alt Context not cancelled
        CM->>Task: Execute task()
        activate Task
        Task-->>CM: Return result/error
        deactivate Task
        CM-->>Client: Return result
    else Context cancelled
        CM-->>Cancel: ctx.Done() triggered
        Cancel-->>Client: Return context.Canceled
    end
    deactivate CM
    end

    rect rgb(255, 240, 220)
    Note over Client,Cancel: Timeout Execution
    Client->>CM: ExecuteWithContextTimeout(ctx, task, timeout)
    activate CM
    CM->>CM: Create timeout context
    CM->>CM: ExecuteWithContext(timeoutCtx, task)
    CM-->>Client: Return (result or timeout error)
    deactivate CM
    end
Loading
sequenceDiagram
    participant Client
    participant CM as ContextManager
    participant Pool as Worker Pool
    participant Worker1 as Worker 1
    participant Worker2 as Worker 2

    rect rgb(220, 255, 220)
    Note over Client,Worker2: Concurrent Item Processing
    Client->>CM: ProcessItemsConcurrently(ctx, items, maxWorkers=2)
    activate CM
    
    CM->>Pool: Create worker pool (2 workers)
    CM->>CM: Distribute items to workers
    
    par Worker Execution
        activate Worker1
        Pool->>Worker1: Process item[0]
        Worker1->>Worker1: Process & return result
        Worker1-->>Pool: Result
        deactivate Worker1
    and
        activate Worker2
        Pool->>Worker2: Process item[1]
        Worker2->>Worker2: Process & return result
        Worker2-->>Pool: Result
        deactivate Worker2
    and
        activate Worker1
        Pool->>Worker1: Process item[2]
        Worker1-->>Pool: Result
        deactivate Worker1
    end
    
    alt All items processed successfully
        Pool-->>CM: Aggregated results
        CM-->>Client: Return []string, nil
    else Error or Context Cancelled
        alt ctx.Done() triggered
            CM-->>Client: Return error (context cancelled)
        else Worker error
            CM-->>Client: Return error (propagated)
        end
    end
    deactivate CM
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% 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 accurately describes the main change—adding a solution for Challenge 30 by a specific contributor.
Description check ✅ Passed The description is directly related to the changeset, explaining the Challenge 30 solution submission and listing the modified file.

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

🤖 Fix all issues with AI Agents
In @challenge-30/submissions/imankhodadi/solution-template.go:
- Around line 252-270: The code currently returns results[:processedCount] on
cancellation which slices by position not by which indices were filled; instead
build and return a slice of only successfully processed values in index order:
after the receive loop (before returning on ctx.Err()) iterate the results
slice, collect indices with non-empty values into an int slice, sort it (add
"sort" to imports), then append results[idx] into a new []string and return that
slice with ctx.Err(); also run gofmt to fix inconsistent spacing.
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 438db3f and 1e4b545.

📒 Files selected for processing (1)
  • challenge-30/submissions/imankhodadi/solution-template.go
🔇 Additional comments (7)
challenge-30/submissions/imankhodadi/solution-template.go (7)

33-43: LGTM!

These wrapper methods correctly delegate to the standard library context functions.


45-61: LGTM!

The GetValue comment correctly documents the nil-value ambiguity. The GetStringValue implementation handles type assertion safely.


63-79: LGTM!

The documentation clearly states the goroutine behavior on cancellation. The buffered channel prevents the goroutine from blocking on send when the context is cancelled.


88-126: LGTM!

Both WaitForCompletion and WaitWithProgress correctly use timers/tickers with proper cleanup via defer. The minimum interval fallback in WaitWithProgress prevents issues with very small durations.


167-190: LGTM!

Good use of pre-allocated slice capacity. The cancellation check pattern is correct, and partial results are properly returned on context cancellation.


290-325: LGTM!

Both ChainOperations and RateLimitedExecution correctly handle context cancellation. The rate limiter correctly skips the initial wait for the first task.


326-342: LGTM!

Good demonstration of the API with proper cleanup using defer cancel().

Comment on lines +252 to +270
results := make([]string, len(items))
processedCount := 0
for result := range resultChan {
if result.err != nil {
cancel() // Signal other workers to stop
// Drain remaining results
for range resultChan {
}
return nil, result.err
}
results[result.index] = result.value
processedCount++
}
// Check if context was cancelled
if ctx.Err() != nil {
// Return only successfully processed results
return results[:processedCount], ctx.Err()
}
return results, 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 | 🟠 Major

Bug: Incorrect result slice on cancellation returns unprocessed items.

When the context is cancelled, results[:processedCount] is returned, but results are indexed by their original position (not by order of completion). This can return empty strings for items that were never processed.

Example: If items at indices 0, 2, 4 complete before cancellation with processedCount=3, then results[:3] returns {processed_item0, "", processed_item2} — the empty string at index 1 was never processed.

🔎 Suggested fix: Return only successfully processed items
 	// Collect results
-	results := make([]string, len(items))
-	processedCount := 0
+	results := make([]string, len(items))
+	processedIndices := make([]int, 0, len(items))
 	for result := range resultChan {
 		if result.err != nil {
 			cancel() // Signal other workers to stop
 			// Drain remaining results
 			for range resultChan {
 			}
 			return nil, result.err
 		}
 		results[result.index] = result.value
-		processedCount++
+		processedIndices = append(processedIndices, result.index)
 	}
 	// Check if context was cancelled
 	if ctx.Err() != nil {
-		// Return only successfully processed results
-		return results[:processedCount], ctx.Err()
+		// Return only successfully processed results in order
+		sort.Ints(processedIndices)
+		successfulResults := make([]string, len(processedIndices))
+		for i, idx := range processedIndices {
+			successfulResults[i] = results[idx]
+		}
+		return successfulResults, ctx.Err()
 	}
 	return results, nil

This requires adding "sort" to imports.

Additionally, there are inconsistent leading spaces on lines 252, 254, 255, 262 — consider running gofmt.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In @challenge-30/submissions/imankhodadi/solution-template.go around lines 252 -
270, The code currently returns results[:processedCount] on cancellation which
slices by position not by which indices were filled; instead build and return a
slice of only successfully processed values in index order: after the receive
loop (before returning on ctx.Err()) iterate the results slice, collect indices
with non-empty values into an int slice, sort it (add "sort" to imports), then
append results[idx] into a new []string and return that slice with ctx.Err();
also run gofmt to fix inconsistent spacing.

@RezaSi RezaSi closed this Jan 7, 2026
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