From dc5c3eda0dddee6ca403f30c1ea4364d9e959912 Mon Sep 17 00:00:00 2001 From: "go-interview-practice-bot[bot]" <230190823+go-interview-practice-bot[bot]@users.noreply.github.com> Date: Sat, 11 Apr 2026 03:41:32 +0000 Subject: [PATCH 1/2] Add solution for Challenge 30 --- .../jrbarbati/solution-template.go | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 challenge-30/submissions/jrbarbati/solution-template.go diff --git a/challenge-30/submissions/jrbarbati/solution-template.go b/challenge-30/submissions/jrbarbati/solution-template.go new file mode 100644 index 000000000..63cb2b59d --- /dev/null +++ b/challenge-30/submissions/jrbarbati/solution-template.go @@ -0,0 +1,135 @@ +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 + + // 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) { + return context.WithCancel(parent) +} + +// CreateTimeoutContext creates a context with timeout +func (cm *simpleContextManager) CreateTimeoutContext(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) { + return context.WithTimeout(parent, timeout) +} + +// AddValue adds a key-value pair to the context +func (cm *simpleContextManager) AddValue(parent context.Context, key, value interface{}) context.Context { + return context.WithValue(parent, key, value) +} + +// GetValue retrieves a value from the context +func (cm *simpleContextManager) GetValue(ctx context.Context, key interface{}) (interface{}, bool) { + 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 { + ch := make(chan error) + + go func() { + ch <- task() + }() + + select { + case err := <- ch: + 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 { + select { + case <- time.After(duration): + return nil + case <- ctx.Done(): + return ctx.Err() + } +} + +// 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 + cm := NewContextManager() + + fmt.Println(description) + + return cm.WaitForCompletion(ctx, workDuration) +} + +// Helper function - process multiple items with context +func ProcessItems(ctx context.Context, items []string) ([]string, error) { + var err error + cm := NewContextManager() + processed := make([]string, 0) + + for _, item := range items { + processed = append(processed, "processed_" + item) + + if err = cm.WaitForCompletion(ctx, 30*time.Millisecond); err != nil { + return processed, err + } + } + + return processed, 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!") +} From f43675ff2a4393f11242a75e85fa8988bd2ce377 Mon Sep 17 00:00:00 2001 From: "go-interview-practice-bot[bot]" <230190823+go-interview-practice-bot[bot]@users.noreply.github.com> Date: Sat, 11 Apr 2026 03:52:58 +0000 Subject: [PATCH 2/2] Add solution for Challenge 30 --- challenge-30/submissions/jrbarbati/solution-template.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/challenge-30/submissions/jrbarbati/solution-template.go b/challenge-30/submissions/jrbarbati/solution-template.go index 63cb2b59d..aeae926d9 100644 --- a/challenge-30/submissions/jrbarbati/solution-template.go +++ b/challenge-30/submissions/jrbarbati/solution-template.go @@ -61,7 +61,7 @@ func (cm *simpleContextManager) GetValue(ctx context.Context, key interface{}) ( // ExecuteWithContext executes a task that can be cancelled via context func (cm *simpleContextManager) ExecuteWithContext(ctx context.Context, task func() error) error { - ch := make(chan error) + ch := make(chan error, 1) go func() { ch <- task() @@ -87,9 +87,6 @@ func (cm *simpleContextManager) WaitForCompletion(ctx context.Context, duration // 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 cm := NewContextManager() fmt.Println(description) @@ -104,11 +101,11 @@ func ProcessItems(ctx context.Context, items []string) ([]string, error) { processed := make([]string, 0) for _, item := range items { - processed = append(processed, "processed_" + item) - if err = cm.WaitForCompletion(ctx, 30*time.Millisecond); err != nil { return processed, err } + + processed = append(processed, "processed_" + item) } return processed, nil