If you are preparing for software engineering interviews, you will almost certainly be asked to design a URL Shortener (like TinyURL or Bitly).
The core of this system design problem relies on understanding two fundamental concepts: Hashing and Encoding.
Here is why simply generating a random string isn't enough, and why engineers use a combination of MD5 and Base64.
Step 1: The Hash (Uniqueness)
If you paste a long URL into a shortener, the system needs a way to guarantee that the same long URL always generates the same short ID. To do this, we use a Hash function like MD5.
MD5 takes any string and turns it into a fixed-length 128-bit hash (usually represented as 32 hex characters).
Example: https://devformattx.vercel.app becomes a5b...
👉 Try generating an MD5 Hash locally here
Step 2: The Encode (Compression)
A 32-character MD5 hash is too long for a 'Tiny' URL. We need to compress it.
This is where Base64 Encoding comes in. Base64 takes the binary data from the hash and represents it using 64 different characters (A-Z, a-z, 0-9, +, /). Because we are using 64 characters instead of just 16 (Hex), we can represent the same data in a much shorter string. We usually grab the first 7 characters of this Base64 string to act as our final Short URL.
👉 Try Encoding a string into Base64 here
The Golden Rule for Interviews
Never confuse Hashing with Encoding!
- Hashing (MD5/SHA-256): One-way. You cannot reverse a hash back to the original URL.
- Encoding (Base64): Two-way. Anyone can decode a Base64 string back to its original form.
Practice running strings through both tools to understand the difference before your next whiteboard interview!