Mock Week 5 — System Design + Coding Combined Session

Sanjeev SharmaSanjeev Sharma
2 min read

Advertisement

Week 5 — System Design + Coding Combo

This format tests both breadth (design) and depth (implementation).


Session Format (60 min)

0:000:15  System design: high-level architecture
0:150:25  Drill into one component
0:250:55  Code the core data structure or algorithm
0:551:00  Review + complexity discussion

Combo 1: Design a URL Shortener → Code the Encoder

Design phase (15 min):

  • Requirements: encode URL, decode short code, handle 100M URLs/day
  • Storage: key-value store (e.g., DynamoDB)
  • Encoding: base-62 counter or hash + truncate
  • Collision handling: retry with increment
  • Read/write ratio: 100:1 → add read cache (Redis)

Code phase (30 min) — Base-62 encoder:

CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

def encode(n: int) -> str:
    res = []
    while n:
        res.append(CHARS[n % 62])
        n //= 62
    return ''.join(reversed(res)) or CHARS[0]

def decode(s: str) -> int:
    n = 0
    for c in s:
        n = n * 62 + CHARS.index(c)
    return n

Combo 2: Design a Cache → Code LRU

Design phase:

  • Distributed cache: consistent hashing across nodes
  • Eviction: LRU within each node
  • Replication: primary + replica for fault tolerance
  • TTL: background thread eviction

Code phase: (see dsa-system-design/01-lru-cache.mdx)


Combo 3: Design a Leaderboard → Code Top-K

Design phase:

  • Real-time leaderboard for 10M players
  • Updates: O(log n) with sorted set (Redis ZADD)
  • Top-K query: O(K log n) with ZREVRANGE
  • Persistence: write-behind to DB

Code phase:

from sortedcontainers import SortedList

class Leaderboard:
    def __init__(self):
        self.scores = {}
        self.sl = SortedList(key=lambda x: -x[0])

    def update(self, player_id, score):
        if player_id in self.scores:
            self.sl.remove((self.scores[player_id], player_id))
        self.scores[player_id] = score
        self.sl.add((score, player_id))

    def top(self, k):
        return [(pid, sc) for sc, pid in self.sl[:k]]

System Design Checklist

Before diving into design, always cover:

1. Clarify requirements
   - Functional: what does it do?
   - Non-functional: scale, latency, availability

2. Estimate scale
   - DAU, QPS, storage per day, bandwidth

3. High-level design
   - ClientLoad BalancerApp ServersDB/Cache

4. Deep dive one component
   - Data model, indexing strategy, sharding

5. Trade-offs
   - SQL vs NoSQL, consistency vs availability, cache invalidation

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro