Katkat

Implement URL Shortening Codec

ShopifyMachine Learning Engineer · Onsite

Last updated

This question evaluates understanding of hashing, encoding, state management, and collision-handling strategies for creating a consistent mapping between long and short URLs, testing competencies in data structures, algorithms, and simple system design within the Coding & Algorithms domain.

Implement a small in-memory URL-shortening component in a pair-programming interview. Expose two methods: shorten(long_url: str) -> str, which returns a short URL under a fixed host such as https://short.example/, and expand(short_url: str) -> str, which returns the original long URL. The same long URL should return the same short URL during one process lifetime, and two different long URLs must never decode to the wrong original URL. You may choose an incrementing ID, random token, hash-based token, or another scheme, but be ready to explain collision handling and trade-offs. Assume no external database or network calls. Follow-ups may include persistence, preventing predictable URLs, concurrent requests, invalid input, and time or space complexity.