Visual Solution
Base62 vs MD5 for Short Code Generation
Compare two approaches for generating short codes: (A) Hash the long URL with MD5 and take the first 7 characters, (B) Use an auto-incrementing counter and encode it in Base62. Which approach is better and why?
Solution PathTarget: 8 min
MD5 hashing seems intuitive but introduces collision risk and retry complexity. Base62 encoding of an auto-incrementing counter is simpler, faster, and guarantees uniqueness with zero collisions.
01Two Approaches
1/4We compare two approaches for generating 7-character short codes: (A) MD5 hash of the long URL, truncated to 7 characters, vs (B) auto-incrementing counter encoded in Base62.
MD5 Hashing vs Base62 Counter: which is better for short code generation?
02MD5 Hashing Analysis
2/4MD5 produces a 128-bit (32 hex character) hash. Truncating to 7 characters loses information and creates collision risk. You must check the database before every insert and retry on collision, adding latency and complexity.
MD5: 128-bit output truncated to 7 chars. Collisions inevitable. Requires DB check + retry on every write.
03Base62 Counter AnalysisKEY INSIGHT
3/4An auto-incrementing counter maps each ID to exactly one unique Base62 string. 62^7 = 3.5 trillion unique codes. Zero collision probability by design. No DB check needed before insert. Simpler, faster, and more predictable.
Base62 Counter: zero collisions, 62^7 = 3.5T capacity, O(1) encode, no DB check needed
04Final Verdict
4/4Base62 counter wins on every dimension: simplicity, performance, and correctness. The only advantage of MD5 (deterministic deduplication of identical URLs) can be handled separately with a lookup index.
Base62 counter is the clear winner. Simpler, faster, collision-free, and predictable.
Concepts from this question1 concepts unlocked
★
EASYBase62 Encoding
An encoding scheme using 62 characters [a-zA-Z0-9] to convert numeric IDs into compact alphanumeric strings. Each character represents a digit in base-62.
Base62 maps auto-incrementing IDs to short, URL-safe strings with zero collision probability. It is the preferred encoding for most URL shortener designs.
url-q3url-q5url-q10
Practice (3 Qs) →Want more practice?
Try more interview questions from this topic →