educative.io

This is regarding system requirement for URL shortening service if the same large URL is given to the system by the same user , should it generate different Unique urls?

This is regarding system requirement for URL shortening service if the same large URL is given to the system by the same user , should it generate different Unique urls?

Great question! This is something that you should clarify with your interviewer. Then the interviewer not knowing the answer will ask you: “What would you do?” and you should be ready to list Pros/Cons and then pick the easiest solution! :slight_smile:

If you lean towards returning the same tinyUrl (previously generated) you should be careful about what to do with the additional (and possibly different) parameters that the API offers. Furthermore, you should be really careful when retrieving the tinyUrl from a raw one. Would you store the other mapping doubling the capacity of the system or try something clever? Will “clever” mean a “maintenance nightmare”?

If you lean towards generating a new unique tinyUrl then your life will probably be easier! :wink:

1 Like

This is a good question to clarify with your interviewer. There are advantages to both but IMO.

#1 return the same key for the same URL
PRO: Save storage space
CONS:

  1. You pretty much have to go with a hashing based solution which has its own problems regarding conflicts
  2. It’s more complicated to implement when you start to thinking about what are identical urls really mean? Like for example, if you have a raw url and an encoded url, should it also return the same key? What about with a URL with and without a protocol?

#2 Generate a new key for every URL
PRO:

  1. Easier to implement
  2. Can easily avoid conflicts with the right implementation
  3. Can allow future enhancements or premium services such are providing analytics. For example if you have a premium service where users pay to get analytics then they’ll want to know how many hits they’re getting from the short URL they paid for. If you were to return the same key for the same URL, two users will get the same URL and there’s no way to know if traffic is coming from their campaign or from somewhere else.

CONS:

  1. Takes up more storage but storage is cheap today
2 Likes

This could be a hard requirement. Imagine if a user wants to shorten a URL twice to share them in two different places. Later the user wants to count which URL got more hits, this is very common with Ad Campaigns.

4 Likes