Showing posts with label tinyurl. Show all posts
Showing posts with label tinyurl. Show all posts

Friday, July 09, 2010

Little Observation about tinyurl

The tinyurl service takes a long URL string (which can be a site address or a document available online) and  return back a permanent shortcut pointing the original source url. For example

http://google.com generates  http://tinyurl.com/2tx
or

http://mail.yahoo.com generates  http://tinyurl.com/6dg
or 
http://tinyurl.com/5zv5vm
or 
http://gilby.com generated http://tinyurl.com/1 

 
Now the question is, how the service creates a new shortcut for all those uncountable number of available? What algorithm is working behind? Is it compressed URL form? Or is it saved/stored in database?


A little logical observation can reveal the little "secrets" working behind.

Currently the first "obvious fact" is that the shortcut has always a fixed length. In fact, the effective length looks as if it is exactly six (last 6 characters) but it has started from single digit  and now it is generating all urls with six digit  until threshold for it is reached then it will move to 7 digits.

The second "obvious fact" is that the shortcut consists of only small-caps letters and the digits 0-9.

The above observations quickly lead to the conclusion that the number of all possible configurations of the second part of the shortcut string is bounded above by 366 = (26 letters + 10 digits)6. This number equals 2176782336 and is close to int primitive data type in java
.
Thus all one has to do is to generate a class that creates a incremental unique alphanumeric number and store the original url reference in db and show it whenever that particular tinyurl is invoked.

Easy Logic,Go implement it.