Introduction
Link shorteners look simple but contain several classic ideas: encoding numbers compactly in base 62, mapping in both directions with hash maps, normalising input so equivalent URLs share a code, and expiring data. This project builds the core service; no network access is involved.
Problem statement
Objectives
- Implement base-62 encoding and decoding
- Generate unique, non-sequential codes with a bijective scramble
- Validate and normalise URLs with java.net.URI
- Maintain two consistent maps (code→link, URL→code)
Features
- Six-character scrambled codes and custom aliases
- URL normalisation and de-duplication
- Expiry, click counts, top links and purge
- Clear errors for invalid URLs and aliases
Concepts demonstrated
- Base conversion
- Bijective mappings
- Normalisation
- Hash maps
- Expiry
System architecture
Base62 converts numbers to and from strings over 62 symbols. Shortener takes a counter, multiplies it by a large odd constant modulo 62⁶ — a bijection, so every counter value gives a different code — and encodes the result as six base-62 digits. normalise uses URI to validate and canonicalise. byCode maps codes to immutable Link records (a click replaces the record with an updated copy); codeByUrl lets repeated URLs reuse their code. purgeExpired removes links from both maps.
Folder structure
Complete source code
src/Base62.java
What each file does
- src/Base62.java
- Base-62 encoding and overflow-checked decoding.
- src/Shortener.java
- Normalisation, code generation, aliases, resolution with clicks, expiry and top links.
- src/Main.java
- The simulated clock and command loop.
How to run it
Compile
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
shorten https://Example.com:443/courses/java?level=beginner shorten https://example.com/courses/java?level=beginner shorten http://docs.example.org/api/Stream.html 5 shorten ftp://files.example.com/x shorten https://localhost/test shorten not-a-url alias java-course https://example.com/courses/java alias java-course https://example.com/other alias x! https://example.com/ open java-course open java-course open java-course open zzzzzz day 7 open 8o9TOO info java-course top 3 purge day 3 quit
Testing
- Run with
tests/sample-input.txtand compare withtests/expected-output.txt. https://Example.com:443/courses/java?...and the lower-case form without the port get the same code.- ftp URLs, hosts without a dot and malformed text are rejected.
- After moving to day 7, the link that expired after day 6 returns 404 and is removed by purge.
Common errors
Two URLs get the same code
Use a bijective mapping (an odd multiplier modulo 62^6) or check for collisions before storing.
Case differences create duplicates
Normalise the scheme and host to lower case before looking up the URL.
Possible improvements
- Persist links to a file
- Expose the service over HTTP with com.sun.net.httpserver
- Add per-day click statistics
Additional challenges
- 1Decode a code back to its counter value and prove the mapping is reversible.
- 2Add a QR-code text renderer for a link.
- 3Rate-limit shortening to 5 links per user per day.
Built and tested it yourself? Mark it to update your dashboard.