go:interview:live_coding_test
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| go:interview:live_coding_test [2026/01/31 01:18] – created phong2018 | go:interview:live_coding_test [2026/01/31 01:22] (current) – phong2018 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | 1️⃣ Worker Pool with Goroutines and Channels | + | ====== Golang Live Coding Exam (Junior → Middle) ====== |
| - | Problem | + | |
| - | Write a program that: | + | This exam evaluates fundamental Go skills, basic concurrency, |
| - | Takes a list of jobs (integers). | + | Time suggestion: 45–60 minutes |
| - | Starts N worker goroutines to process the jobs concurrently. | + | ===== Test 1: Sum of Integers ===== |
| - | Each job simulates work using time.Sleep | + | ==== Question ==== |
| + | Write a function that receives a list of integers | ||
| - | Collects and prints results safely (no data races). | + | ==== Input ==== |
| + | < | ||
| + | [1, 2, 3, 4, 5] | ||
| + | </ | ||
| - | What this tests | + | ==== Output ==== |
| + | < | ||
| + | 15 | ||
| + | </ | ||
| - | Goroutines and channels | + | ==== Constraints ==== |
| - | sync.WaitGroup | + | Do not use external libraries |
| - | Concurrency design (worker pool pattern) | + | Must handle empty slices |
| - | Senior extensions | + | ==== Evaluation Criteria ==== |
| - | Add context.Context to support cancellation | + | Correct result |
| - | Handle job timeouts | + | Clean and readable code |
| - | Graceful shutdown | + | Proper function signature |
| - | 📘 Notes | + | ==== Notes ==== |
| - | Worker pool /ˈwɜːrkər puːl/: a fixed number of workers processing tasks from a queue | + | Slice /slaɪs/: dynamic array in Go |
| - | Data race /ˈdeɪtə reɪs/: concurrent access to shared data without synchronization | + | ===== Test 2: Word Frequency Counter ===== |
| - | 2️⃣ Rate Limiter (Token Bucket) | + | ==== Question ==== |
| - | Problem | + | Given a string, count how many times each word appears. |
| - | Implement a rate limiter that: | + | Words are separated by spaces. |
| - | Allows at most X requests per second | + | ==== Input ==== |
| + | < | ||
| + | "go is fun and go is fast" | ||
| + | </ | ||
| - | Blocks or rejects requests when the limit is exceeded | + | ==== Output ==== |
| + | < | ||
| + | go: 2 | ||
| + | is: 2 | ||
| + | fun: 1 | ||
| + | and: 1 | ||
| + | fast: 1 | ||
| + | </ | ||
| - | Is safe for concurrent use | + | ==== Constraints ==== |
| - | What this tests | + | Case-sensitive |
| - | Time-based logic | + | Use built-in data structures only |
| - | Concurrency safety | + | ==== Evaluation Criteria ==== |
| - | Understanding of rate-limiting algorithms | + | Correct map usage |
| - | Senior extensions | + | Proper string manipulation |
| - | Use time.Ticker | + | Clean iteration logic |
| - | Support burst traffic | + | ==== Notes ==== |
| - | Make it configurable | + | Map /mæp/: key-value data structure |
| - | 📘 Notes | + | Iteration / |
| - | Rate limiter /reɪt ˈlɪmɪtər/ | + | ===== Test 3: Filter Even Numbers ===== |
| - | Token bucket /ˈtoʊkən ˈbʌkɪt/: algorithm using tokens to allow requests | + | ==== Question ==== |
| + | Write a function that returns only even numbers from a list. | ||
| - | 3️⃣ Concurrent URL Fetcher with Error Handling | + | ==== Input ==== |
| - | Problem | + | < |
| + | [1, 2, 3, 4, 5, 6] | ||
| + | </ | ||
| - | Write a function that: | + | ==== Output ==== |
| + | < | ||
| + | [2, 4, 6] | ||
| + | </ | ||
| - | Accepts a list of URLs | + | ==== Constraints ==== |
| - | Fetches them concurrently | + | Maintain input order |
| - | Limits concurrency to N requests at a time | + | No global variables |
| - | Returns results and errors separately | + | ==== Evaluation Criteria ==== |
| - | What this tests | + | Correct condition logic |
| - | Goroutine orchestration | + | Proper slice handling |
| - | Channel fan-in / fan-out | + | ==== Notes ==== |
| - | Error handling in concurrent systems | + | Even number /ˈiːvən ˈnʌmbər/: |
| - | Senior extensions | + | ===== Test 4: Goroutine with Channel ===== |
| - | Add retry with backoff | + | ==== Question ==== |
| + | Create a goroutine that sends numbers from 1 to 3 into a channel. | ||
| + | The main function must receive and print them. | ||
| - | Use context.Context for cancellation | + | ==== Input ==== |
| + | < | ||
| + | 1, 2, 3 | ||
| + | </ | ||
| - | Stop early if too many failures occur | + | ==== Output ==== |
| + | < | ||
| + | 1 | ||
| + | 2 | ||
| + | 3 | ||
| + | </ | ||
| - | 📘 Notes | + | ==== Constraints ==== |
| - | Fan-in / Fan-out /fæn ɪn | fæn aʊt/: merging and splitting concurrent flows | + | Must use one goroutine |
| - | Backoff / | + | Channel must be closed properly |
| - | 4️⃣ Thread-safe In-Memory Cache with TTL | + | ==== Evaluation Criteria ==== |
| - | Problem | + | |
| - | Implement an in-memory cache that: | + | Correct goroutine usage |
| - | Stores key-value pairs | + | Proper channel closing |
| - | Supports TTL (time to live) | + | No deadlocks |
| - | Is safe for concurrent reads and writes | + | ==== Notes ==== |
| - | What this tests | + | Goroutine / |
| - | sync.Mutex | + | Deadlock |
| - | Data consistency | + | ===== Test 5: Simple HTTP Handler ===== |
| - | Time-based eviction | + | ==== Question ==== |
| + | Create an HTTP server that: | ||
| - | Senior extensions | + | Listens on port 8080 |
| - | Background cleanup goroutine | + | Responds "Hello Go" to /hello |
| - | LRU eviction strategy | + | ==== Input ==== |
| + | < | ||
| + | GET /hello | ||
| + | </ | ||
| - | Metrics (hits/misses) | + | ==== Output ==== |
| + | < | ||
| + | Hello Go | ||
| + | </code> | ||
| - | 📘 Notes | + | ==== Constraints ==== |
| - | TTL (time to live) /ˌtiː tiː ˈɛl/: how long data stays valid | + | Use net/http |
| - | Eviction / | + | No third-party libraries |
| - | 5️⃣ Graceful HTTP Server Shutdown | + | ==== Evaluation Criteria ==== |
| - | Problem | + | |
| + | Correct handler implementation | ||
| + | |||
| + | Proper server startup | ||
| + | |||
| + | Clean code structure | ||
| + | |||
| + | ==== Notes ==== | ||
| + | |||
| + | HTTP handler /ˌeɪtʃ tiː tiː piː ˈhændlər/: | ||
| + | |||
| + | ====== Senior Golang Live Coding Exam ====== | ||
| + | |||
| + | This exam contains 5 live coding tests for Senior Golang developers. | ||
| + | Candidates are expected to write correct, concurrent-safe, | ||
| + | |||
| + | Time suggestion: 60–90 minutes | ||
| + | |||
| + | ===== Test 1: Worker Pool Processing ===== | ||
| + | |||
| + | ==== Question ==== | ||
| + | You are given a list of jobs represented by integers. | ||
| + | Create a worker pool with N workers that processes the jobs concurrently. | ||
| + | |||
| + | Each job must: | ||
| + | |||
| + | Sleep for 100ms | ||
| + | |||
| + | Return jobID * 2 | ||
| + | |||
| + | The program must print the result for each job. | ||
| + | |||
| + | ==== Input ==== | ||
| + | |||
| + | jobs = [1, 2, 3, 4, 5] | ||
| + | |||
| + | workers = 2 | ||
| + | |||
| + | ==== Output ==== | ||
| + | Order does NOT matter. | ||
| + | |||
| + | < | ||
| + | |||
| + | ==== Constraints ==== | ||
| + | |||
| + | Must use goroutines and channels | ||
| + | |||
| + | Must not have data races | ||
| + | |||
| + | Workers must exit cleanly | ||
| + | |||
| + | ==== Evaluation Criteria ==== | ||
| + | |||
| + | Correct concurrency model | ||
| + | |||
| + | No race conditions | ||
| + | |||
| + | Clean shutdown logic | ||
| + | |||
| + | ==== Notes ==== | ||
| + | |||
| + | Worker pool / | ||
| + | |||
| + | Data race /ˈdeɪtə reɪs/: unsafe concurrent memory access | ||
| + | |||
| + | ===== Test 2: Rate Limiter ===== | ||
| + | |||
| + | ==== Question ==== | ||
| + | Implement a rate limiter that allows at most 2 requests per second. | ||
| + | |||
| + | Each call to Allow() returns: | ||
| + | |||
| + | true if the request is allowed | ||
| + | |||
| + | false otherwise | ||
| + | |||
| + | ==== Input ==== | ||
| + | Call times (milliseconds): | ||
| + | |||
| + | < | ||
| + | |||
| + | ==== Output ==== | ||
| + | < | ||
| + | true | ||
| + | true | ||
| + | false | ||
| + | false | ||
| + | false | ||
| + | </ | ||
| + | |||
| + | ==== Constraints ==== | ||
| + | |||
| + | Must be safe for concurrent use | ||
| + | |||
| + | Must rely on time-based logic | ||
| + | |||
| + | Busy-waiting is not allowed | ||
| + | |||
| + | ==== Evaluation Criteria ==== | ||
| + | |||
| + | Correct rate limiting | ||
| + | |||
| + | Thread-safe implementation | ||
| + | |||
| + | Efficient time handling | ||
| + | |||
| + | ==== Notes ==== | ||
| + | |||
| + | Rate limiter /reɪt ˈlɪmɪtər/: | ||
| + | |||
| + | Busy-waiting /ˈbɪzi ˈweɪtɪŋ/: | ||
| + | |||
| + | ===== Test 3: Concurrent URL Fetcher ===== | ||
| + | |||
| + | ==== Question ==== | ||
| + | Write a function that fetches multiple URLs concurrently, | ||
| + | |||
| + | ==== Input ==== | ||
| + | |||
| + | URLs: | ||
| + | |||
| + | < | ||
| + | |||
| + | https:// | ||
| + | |||
| + | https:// | ||
| + | |||
| + | https:// | ||
| + | |||
| + | </ | ||
| + | |||
| + | Max concurrency = 2 | ||
| + | |||
| + | ==== Output ==== | ||
| + | < | ||
| + | success: https:// | ||
| + | |||
| + | success: https:// | ||
| + | |||
| + | success: https:// | ||
| + | |||
| + | </ | ||
| + | |||
| + | (Order does NOT matter) | ||
| + | |||
| + | ==== Constraints ==== | ||
| + | |||
| + | At most 2 HTTP requests at the same time | ||
| + | |||
| + | Must wait for all requests to finish | ||
| + | |||
| + | Errors must not crash the program | ||
| + | |||
| + | ==== Evaluation Criteria ==== | ||
| + | |||
| + | Correct concurrency limit | ||
| + | |||
| + | Proper error handling | ||
| + | |||
| + | No goroutine leaks | ||
| + | |||
| + | ==== Notes ==== | ||
| + | |||
| + | Concurrency limit / | ||
| + | |||
| + | Goroutine leak / | ||
| + | |||
| + | ===== Test 4: Thread-Safe Cache with TTL ===== | ||
| + | |||
| + | ==== Question ==== | ||
| + | Implement an in-memory cache with expiration support. | ||
| + | |||
| + | Each key: | ||
| + | |||
| + | Has a value | ||
| + | |||
| + | Expires after TTL (seconds) | ||
| + | |||
| + | ==== Input ==== | ||
| + | < | ||
| + | Set(" | ||
| + | Get(" | ||
| + | Get(" | ||
| + | </ | ||
| + | |||
| + | ==== Output ==== | ||
| + | < | ||
| + | 1 | ||
| + | nil | ||
| + | </ | ||
| + | |||
| + | ==== Constraints ==== | ||
| + | |||
| + | Concurrent access must be safe | ||
| + | |||
| + | Expired keys must not be returned | ||
| + | |||
| + | No global variables | ||
| + | |||
| + | ==== Evaluation Criteria ==== | ||
| + | |||
| + | Correct TTL handling | ||
| + | |||
| + | Proper locking strategy | ||
| + | |||
| + | Clean code structure | ||
| + | |||
| + | ==== Notes ==== | ||
| + | |||
| + | TTL (Time To Live) /ˌtiː tiː ˈɛl/: duration data is valid | ||
| + | |||
| + | Thread-safe /θred seɪf/: safe for concurrent use | ||
| + | |||
| + | ===== Test 5: Graceful HTTP Server Shutdown ===== | ||
| + | |||
| + | ==== Question ==== | ||
| Create an HTTP server that: | Create an HTTP server that: | ||
| - | Handles requests normally | + | Listens on port 8080 |
| + | |||
| + | Responds " | ||
| + | |||
| + | Shuts down gracefully on SIGINT | ||
| + | |||
| + | ==== Input ==== | ||
| + | |||
| + | HTTP request: GET /health | ||
| - | Listens for OS signals (SIGINT, SIGTERM) | + | OS signal: |
| - | Shuts down gracefully without dropping in-flight requests | + | ==== Output ==== |
| + | < | ||
| + | OK | ||
| + | Server shutdown completed | ||
| + | </ | ||
| - | What this tests | + | ==== Constraints ==== |
| - | Real-world Go server knowledge | + | In-flight requests must complete |
| - | Signal handling | + | Shutdown timeout must be handled |
| - | http.Server.Shutdown | + | No forced exit (os.Exit) |
| - | Senior extensions | + | ==== Evaluation Criteria ==== |
| - | Add request context cancellation | + | Proper signal handling |
| - | Log active connections during shutdown | + | Correct use of http.Server.Shutdown |
| - | Add timeout handling | + | Clean resource release |
| - | 📘 Notes | + | ==== Notes ==== |
| - | Graceful shutdown / | + | Graceful shutdown / |
| - | In-flight request /ɪn flaɪt rɪˈkwɛst/: | + | In-flight request /ɪn flaɪt rɪˈkwɛst/: |
go/interview/live_coding_test.1769822327.txt.gz · Last modified: by phong2018
