Prepin
Log in

Create a Tiny URL

Approach / Explanation

The prompt requires you to create a URL shortening service, commonly known as 'TinyURL'. This service enables users to input a lengthy URL and transform it into a shorter one that redirects to the original URL. Such services are frequently utilized to fit a URL into limited space, avoid URL breakage in emails, or simply to make URLs more memorable or easier to share. When addressing this type of question in an interview, you should discuss several important components: URL generation method: You should explain how the unique short URL will be created. Scalability: The design must accommodate a high volume of URL mappings and be easily scalable. Database structure: You need to specify how and where the mapping between the short URL and the original URL will be stored. API design: You should outline how the APIs will operate.

Suggested Answer

An example response for designing a TinyURL could be as follows: A straightforward method for developing this system might involve taking an input URL and hashing it to create a unique value. This shortened hash could then serve as the TinyURL. However, this method could generate longer URLs for shorter input URLs, and there's a risk that different URLs might produce the same shortened URL due to variations in the hashing mechanism. A more effective design strategy would be to utilize a counter. By encoding the current counter, we can use it as an alias for the long URL while maintaining the mapping in a database. Nevertheless, this approach would lead to URLs of increasing length as more URLs are added over time. In terms of database design, a Hash Table could be implemented, where the ‘key’ represents the ‘alias’ (generated through the encoding process) and the value corresponds to the actual URL. For the API design, a RESTful API would be suitable, featuring two endpoints - one for generating the TinyURL and another for redirecting. The first API takes in the long URL and produces a TinyURL, while the second API takes the TinyURL and redirects to the associated long URL stored in the system.

Alternative Answer

Alternatively, the system could utilize a short random alphanumeric string as a unique identifier for each URL, addressing the problem of increasingly lengthy URL strings seen in the previous method. A user would submit a URL, and the service would generate a random alphanumeric string consisting of 6 or 7 characters. This approach allows the service to accommodate up to (26+26+10) ^6 for a 6-character identifier (over 56 billion URLs) or (26+26+10) ^7 for a 7-character identifier, offering significant scalability. To store this information, a database with quick lookup capabilities would be optimal. A key-value store like Redis or a NoSQL database such as MongoDB would be suitable for this purpose. The API design would mirror that of example 1, employing a RESTful API with two endpoints: one for generating the tiny URL and another for redirecting. The first API takes in the long URL and returns a tiny URL, while the second API takes the tiny URL and redirects to the relevant long URL stored in the system.

Question Details

Difficulty & Category

Medium
system design

Companies

Amazon

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.