Practice
🧠 I. BÀI TOÁN KINH ĐIỂN VỀ CONCURRENCY (Goroutine + Channel)
Mục tiêu: Kiểm tra hiểu biết sâu về goroutine, channel, select, sync, race condition, context, worker pool.
1️⃣ Worker Pool Pattern
Đề: Có 100 task (số), tạo 5 worker xử lý song song, in ra kết quả theo thứ tự. Kiểm tra: channel, goroutine, sync.WaitGroup, buffered channel.
func worker(id int, jobs <-chan int, results chan<- int, wg *sync.WaitGroup) {
defer wg.Done()
for j := range jobs {
fmt.Printf("Worker %d processing job %d\n", id, j)
time.Sleep(time.Second)
results <- j * 2
}
}2️⃣ Producer - Consumer
Đề: Có goroutine sản xuất dữ liệu, goroutine tiêu thụ dữ liệu, dừng đúng lúc (context hoặc close channel). Kiểm tra: phối hợp goroutine, context cancel, select.
3️⃣ Fan-out / Fan-in Pattern
Đề: Một input → nhiều worker xử lý → gom kết quả về một channel. Kiểm tra: closing channels đúng cách, tránh leak goroutine.
4️⃣ Ping-Pong / Passing Ball
Đề: 2 (hoặc nhiều) goroutine “chuyền bóng” qua channel.
Kiểm tra: hiểu cơ chế <-chan, chan<-, blocking send/receive.
5️⃣ Context Timeout / Cancelation
Đề: Chạy một tác vụ, nếu quá 5s thì cancel.
Kiểm tra: context.WithTimeout, select và tránh leak.
6️⃣ Safe Counter / Race Condition
Đề: Tạo counter chạy trong nhiều goroutine, đảm bảo an toàn.
Kiểm tra: sync.Mutex, sync/atomic, hoặc chan để serialize.
🧩 II. BÀI TOÁN VỀ STRUCT, INTERFACE, VÀ DESIGN PATTERN
Mục tiêu: Đánh giá khả năng thiết kế hệ thống, hiểu interface & struct composition.
7️⃣ Implement Interface
Đề: Viết interface Shape có method Area(); implement Circle, Rectangle.
Kiểm tra: interface, receiver (value vs pointer), polymorphism.
8️⃣ Dependency Injection
Đề: Tạo struct có field là interface, inject implementation runtime. Kiểm tra: thiết kế linh hoạt, testing dễ.
9️⃣ Singleton Pattern
Đề: Đảm bảo chỉ có 1 instance duy nhất của struct (vd: DB connection).
Kiểm tra: sync.Once.
🔟 Strategy Pattern
Đề: Tạo nhiều “chiến lược” tính toán (e.g. discount algorithm). Kiểm tra: dùng interface, tách logic, open/closed principle.
⚙️ III. BÀI TOÁN THUẬT TOÁN / DATA STRUCTURE
Thường interviewer muốn xem bạn biết DSA cơ bản + áp dụng tư duy Go.
11️⃣ Reverse String / Array / Linked List
→ Câu kinh điển, test slice manipulation, rune handling (UTF-8).
12️⃣ Fibonacci bằng goroutine
→ Tạo pipeline sinh dãy số Fibonacci.
13️⃣ Merge Sorted Arrays
→ Duyệt 2 slice song song, merge theo thứ tự.
14️⃣ Map Reduce kiểu Golang
→ Tạo nhiều goroutine xử lý, gom kết quả.
15️⃣ Cache với TTL
→ Dùng map + sync.RWMutex + time.AfterFunc mô phỏng Redis cache expire.
🧰 IV. BÀI TOÁN HỆ THỐNG / THỰC TẾ BACKEND
Dành cho vị trí Senior/Backend Engineer, interviewer test “how Go handles real-world load”.
16️⃣ Throttling / Rate Limiter
→ Viết limiter bằng channel + time.Ticker (leaky bucket hoặc token bucket).
17️⃣ Graceful Shutdown
→ Khi nhận signal (SIGINT), stop HTTP server, close goroutine.
Kiểm tra: os.Signal, context.WithCancel.
18️⃣ Retry Logic với Backoff
→ Viết function retry 3 lần khi gọi API fail, mỗi lần chờ lâu hơn.
19️⃣ Custom Middleware cho HTTP server
→ Kiểm tra net/http, http.HandlerFunc, closure.
20️⃣ Concurrent Safe Map
→ Viết lại map safe cho concurrency dùng sync.RWMutex.
🧠 V. NÂNG CAO (cho Senior / System Design Go)
Viết in-memory queue (có retry, ack, delay).
Mô phỏng Pub/Sub giữa nhiều subscriber bằng channel.
Implement Worker Retry Pool có priority queue.
Dùng
sync.Condmô phỏng barrier synchronization.Viết Mini EventBus cho microservices (register, emit, broadcast).
🏁 Tóm tắt nhóm bài nên luyện
Concurrency
goroutine, channel, select, context
Đồng bộ & tư duy concurrent
Struct / Interface
design pattern, polymorphism
OOP in Go
Algorithmic
DSA + slice, map
Logic & tư duy tối ưu
System / Backend
HTTP, sync, cache, signal
Thực chiến backend
Last updated