Facebook Interview Question
Software Engineer / DevelopersCountry: United States
If people think allowing same url to map to multiple IDs might lead to a (D)DOS attack, consuming the id space, think again.
DDOS attack is a different thing, i think the service provider takes care of such things when considering multiple ids for same url ( complete consumption of id space is a rare case, DDOS attack will have to be of huge order and extremely fast to do that ). Moreover i already specified that some sites don't add multiple id's for same url e.g tinyurl while some sites do e.g google url shortener.
Little more info from google URL shortener :
The Google URL Shortener provides different functionality based on whether or not you are signed in:
: If you are signed in to goo.gl using a Google Account, a unique short URL is generated each time a long URL is shortened.
: If you are not signed in to goo.gl, the same short URL is reused each time a long URL is shortened, across multiple users
I guess we can apply hashmap funda here............take the sitename as input to hasfunction ...the output of the hashfunction is your tinyURL and map it to the IP address in the hashtable..... :-/
Everytime when a url is to be shortened, url_id field is incremented, url_id is converted to base-36 ( 26 alphabets + 10 digits ) OR base-62 ( 26 small alphabets + 26 capital alphabets + 10 digits ) which serves as primary key for each tuple. A string i.e. the actual url is added corresponding to this key in database. The primary key is appended to service providers domain name after '/' and returned to the user.
- Cerberuz September 13, 2012Usually its better to add a new url_id rather than searching for existence of a url in database. So same url can be shortened to multiple short url's.
But some sites do take care of not adding multiple short url's in database if same user try to reproduce it. They consider user location for this purpose.