Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
c0d1d68
Add solution for Challenge 1 by t4e1
t4e1 Aug 5, 2025
547250f
Add solution for Challenge 2 by t4e1
t4e1 Aug 6, 2025
c772c5f
Add solution for Challenge 3 by t4e1
t4e1 Aug 7, 2025
766eac1
Add solution for Challenge 6 by t4e1
t4e1 Aug 8, 2025
d9cc9c4
Add solution for Challenge 18 by t4e1
t4e1 Aug 9, 2025
dcf9e49
Add solution for Challenge 21 by t4e1
t4e1 Aug 10, 2025
9f7ece7
Add solution for Challenge 22 by t4e1
t4e1 Aug 13, 2025
c062ea1
Add solution for Challenge 4 by t4e1
t4e1 Aug 14, 2025
2fb4b31
Add solution for Challenge 10 by t4e1
t4e1 Aug 16, 2025
c3e3451
Add solution for Challenge 5 by t4e1
Aug 17, 2025
1f9d49d
Add solution for Challenge 7 by t4e1
Aug 18, 2025
61f7228
Add solution for Challenge 13 by t4e1
Aug 20, 2025
af3f014
Add solution for Challenge 17 by t4e1
t4e1 Aug 21, 2025
45470a5
Merge branch 'main' into main
RezaSi Aug 22, 2025
9edeeed
Add solution for Challenge 19 by t4e1
t4e1 Aug 25, 2025
74623c7
Merge branch 'main' of https://github.com/t4e1/go-interview-practice
t4e1 Aug 25, 2025
2bc177f
Add solution for Challenge 27 by t4e1
t4e1 Aug 31, 2025
2704077
Merge branch 'main' into main
RezaSi Sep 2, 2025
99d07a0
📊 Update scoreboards for: challenge-5 challenge-6
actions-user Sep 2, 2025
b454177
🏆 Auto-update: Profile badges regenerated
actions-user Sep 2, 2025
444f562
📊 Update scoreboards for: challenge-27
actions-user Sep 2, 2025
761578a
Auto-update main scoreboard
actions-user Sep 2, 2025
573299a
🏆 Auto-update: Profile badges regenerated
actions-user Sep 2, 2025
541ce4e
Add solution for Challenges by PolinaSvet (#342)
PolinaSvet Sep 2, 2025
116f671
📊 Update scoreboards for: challenge-24 challenge-25 challenge-26 chal…
actions-user Sep 2, 2025
49258f0
Auto-update main scoreboard
actions-user Sep 2, 2025
faa7fc2
🏆 Auto-update: Profile badges regenerated
actions-user Sep 2, 2025
932c2a6
Add solution for Challenges by Gandook (#343)
Gandook Sep 3, 2025
e3ce203
📊 Update scoreboards for: challenge-26 challenge-5
actions-user Sep 3, 2025
b224b8b
Auto-update main scoreboard
actions-user Sep 3, 2025
4ca9b75
🏆 Auto-update: Profile badges regenerated
actions-user Sep 3, 2025
b55f9e7
Add solution for Challenge 17 by longbui98 (#339)
longbui98 Sep 4, 2025
a909ee4
📊 Update scoreboards for: challenge-17
actions-user Sep 4, 2025
d8b2120
🏆 Auto-update: Profile badges regenerated
actions-user Sep 4, 2025
955dbab
Add solution for Challenge 18 by longbui98 (#340)
longbui98 Sep 4, 2025
e3e5d24
📊 Update scoreboards for: challenge-18
actions-user Sep 4, 2025
f83ba94
🏆 Auto-update: Profile badges regenerated
actions-user Sep 4, 2025
6c793c1
Add solution for Challenge 19 by longbui98 (#341)
longbui98 Sep 4, 2025
0e25884
📊 Update scoreboards for: challenge-19
actions-user Sep 4, 2025
fc3233a
🏆 Auto-update: Profile badges regenerated
actions-user Sep 4, 2025
4aef9af
Add solution for Challenge 1 by jvllmr (#357)
jvllmr Sep 4, 2025
d469052
📊 Update scoreboards for: challenge-1
actions-user Sep 4, 2025
a8bb00c
Auto-update main scoreboard
actions-user Sep 4, 2025
824d84d
Add solution for Challenges by wxai2324 and add single-array comparis…
wxai2324 Sep 4, 2025
f121aae
📊 Update scoreboards for: challenge-1 challenge-18 challenge-2 challe…
actions-user Sep 4, 2025
e15e9d4
Auto-update main scoreboard
actions-user Sep 4, 2025
53f10f1
🏆 Auto-update: Profile badges regenerated
actions-user Sep 4, 2025
e634465
Update README.md
RezaSi Sep 4, 2025
04f0a08
Add solution for Challenge 1 by malakagl (#358)
malakagl Sep 5, 2025
96f2eb5
📊 Update scoreboards for: challenge-1
actions-user Sep 5, 2025
db6ac3c
Auto-update main scoreboard
actions-user Sep 5, 2025
32e3dd9
Add solution for Challenges by Kesha005 (#345)
Kesha005 Sep 7, 2025
8fa331b
📊 Update scoreboards for: challenge-1 challenge-18
actions-user Sep 7, 2025
ea66788
Add solution for Challenge 21 by longbui98 (#346)
longbui98 Sep 7, 2025
20130cb
🏆 Auto-update: Profile badges regenerated
actions-user Sep 7, 2025
dbde3f0
📊 Update scoreboards for: challenge-21
actions-user Sep 7, 2025
f8cd427
🏆 Auto-update: Profile badges regenerated
actions-user Sep 7, 2025
0968c1c
Add solution for Challenge 13 by Cpoing (#347)
Cpoing Sep 7, 2025
cdc5700
📊 Update scoreboards for: challenge-13
actions-user Sep 7, 2025
8d18b35
🏆 Auto-update: Profile badges regenerated
actions-user Sep 7, 2025
563d942
Add solution for Challenge 30 by t4e1
t4e1 Sep 7, 2025
e859507
Merge branch 'main' into t4e1/remove-conflict
t4e1 Sep 8, 2025
a06f8a6
Merge branch 'main' into t4e1/remove-conflict
RezaSi Oct 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions badges/PolinaSvet.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions badges/malakagl.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
161 changes: 161 additions & 0 deletions challenge-30/submissions/t4e1/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package main

import (
"context"
"fmt"
"time"
)

// ContextManager defines a simplified interface for basic context operations
type ContextManager interface {
// Create a cancellable context from a parent context
CreateCancellableContext(parent context.Context) (context.Context, context.CancelFunc)

// Create a context with timeout
CreateTimeoutContext(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc)

// Add a value to context
AddValue(parent context.Context, key, value interface{}) context.Context

// Get a value from context
GetValue(ctx context.Context, key interface{}) (interface{}, bool)

// Execute a task with context cancellation support
ExecuteWithContext(ctx context.Context, task func() error) error
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

Make tasks cancellable; pass ctx into the task to prevent goroutine leaks

Current design can’t stop a long-running task; the goroutine keeps executing after ctx is done. Pass ctx to the task and propagate cancellation.

- // Execute a task with context cancellation support
- ExecuteWithContext(ctx context.Context, task func() error) error
+ // Execute a task; task must observe ctx for cancellation.
+ ExecuteWithContext(ctx context.Context, task func(context.Context) error) error
-func (cm *simpleContextManager) ExecuteWithContext(ctx context.Context, task func() error) error {
+func (cm *simpleContextManager) ExecuteWithContext(ctx context.Context, task func(context.Context) error) error {
   errCh := make(chan error, 1)
   go func() {
-    errCh <- task()
+    errCh <- task(ctx)
   }()
   select {
   case err := <-errCh:
     return err
   case <-ctx.Done():
     return ctx.Err()
   }
 }

Also applies to: 74-91

🤖 Prompt for AI Agents
In challenge-30/submissions/t4e1/solution-template.go around lines 24 and 74-91,
the task function currently has signature func() error so long-running tasks
cannot be cancelled and goroutines leak after ctx is done; change
ExecuteWithContext to accept task func(ctx context.Context) error, update all
internal goroutine launches to pass the same ctx, and modify the select/err
propagation so the goroutine returns promptly when ctx.Done() is closed (i.e.,
check ctx in the task or select inside the worker and return a
canceled/appropriate error), and update every call site within lines 74-91 to
pass ctx into the task closures and propagate the returned error back to caller.


// Wait for context cancellation or completion
WaitForCompletion(ctx context.Context, duration time.Duration) error
}

// Simple context manager implementation
type simpleContextManager struct{}

// NewContextManager creates a new context manager
func NewContextManager() ContextManager {
return &simpleContextManager{}
}

// CreateCancellableContext creates a cancellable context
func (cm *simpleContextManager) CreateCancellableContext(parent context.Context) (context.Context, context.CancelFunc) {
// TODO: Implement cancellable context creation
// Hint: Use context.WithCancel(parent)
ctx, cancel := context.WithCancel(parent)
return ctx, cancel
}

// CreateTimeoutContext creates a context with timeout
func (cm *simpleContextManager) CreateTimeoutContext(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
// TODO: Implement timeout context creation
// Hint: Use context.WithTimeout(parent, timeout)
ctx, cancel := context.WithTimeout(parent, timeout)
return ctx, cancel
}

// AddValue adds a key-value pair to the context
func (cm *simpleContextManager) AddValue(parent context.Context, key, value interface{}) context.Context {
// TODO: Implement value context creation
// Hint: Use context.WithValue(parent, key, value)
ctx := context.WithValue(parent, key, value)
return ctx
}

// GetValue retrieves a value from the context
func (cm *simpleContextManager) GetValue(ctx context.Context, key interface{}) (interface{}, bool) {
// TODO: Implement value retrieval from context
// Hint: Use ctx.Value(key) and check if it's nil
// Return the value and a boolean indicating if it was found
if val := ctx.Value(key); val != nil {
return val, true
}
return nil, false
}

// ExecuteWithContext executes a task that can be cancelled via context
func (cm *simpleContextManager) ExecuteWithContext(ctx context.Context, task func() error) error {
// TODO: Implement task execution with context cancellation
// Hint: Run the task in a goroutine and use select with ctx.Done()
// Return context error if cancelled, task error if task fails
errCh := make(chan error, 1)

go func() {
errCh <- task()
}()

select {
case err := <-errCh:
return err
case <-ctx.Done():
return ctx.Err()
}

}

// WaitForCompletion waits for a duration or until context is cancelled
func (cm *simpleContextManager) WaitForCompletion(ctx context.Context, duration time.Duration) error {
// TODO: Implement waiting with context awareness
// Hint: Use select with ctx.Done() and time.After(duration)
// Return context error if cancelled, nil if duration completes
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(duration):
return nil
}
}

// Helper function - simulate work that can be cancelled
func SimulateWork(ctx context.Context, workDuration time.Duration, description string) error {
// TODO: Implement cancellable work simulation
// Hint: Use select with ctx.Done() and time.After(workDuration)
// Print progress messages and respect cancellation
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(workDuration):
fmt.Println(description)
return nil
}
}

// Helper function - process multiple items with context
func ProcessItems(ctx context.Context, items []string) ([]string, error) {
// TODO: Implement batch processing with context awareness
// Process each item but check for cancellation between items
// Return partial results if cancelled
result := make([]string, 0, len(items))

for _, v := range items {
select {
case <-ctx.Done():
return result, ctx.Err()
default:
}

time.Sleep(50 * time.Millisecond)

processedItem := fmt.Sprint("processed_", v)
result = append(result, processedItem)
}

return result, nil
}

// Example usage
func main() {
fmt.Println("Context Management Challenge")
fmt.Println("Implement the context manager methods!")

// Example of how the context manager should work:
cm := NewContextManager()

// Create a cancellable context
ctx, cancel := cm.CreateCancellableContext(context.Background())
defer cancel()

// Add some values
ctx = cm.AddValue(ctx, "user", "alice")
ctx = cm.AddValue(ctx, "requestID", "12345")

// Use the context
fmt.Println("Context created with values!")
}