Amazon Interview Question for SDE-2s

Country: India
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
of 10 vote

I think this is more of a design question than a coding one.

We can think about
- reducing the response time of the server
---- by using a distributed system to share the load based on geography
---- by using a central server but many caching servers at various geographical locations
---- what would be the right database design

- reducing the storage space
---- database design

- backup and failover

- security issues
---- prevent people from creating links to ---whatever---

- handling old/obsolete urls
---- may be, while creating the url we can say to the user that it will be deleted if the url is never used for more than say 3 years
---- may be allow the user to login and delete unused ones

- how a company like is going to make profit out of this service! how can that be improved

- user friendly things
---- browser plugins to speed up creating links (youtube sharing has an option to create short urls)
---- giving report to user about the usage statistics
---- mobile app to create urls quickly


- ramprasad85 February 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
of 9 vote

Redirect Part:

When you click on a link of any tiny url, an HTTP Request is sent to their server with the full URL, like http // / b9 (not a real one).

They read the path part (here b9), which maps to their Database.

In the Database, they find the real URL. Then they issue a redirect, which is a HTTP 302 response and the target URL in the header.

Encoding Part:

One of the most popular URL shortening services simply take the ID in the database of the URL and then convert it to Base 62[a-zA-Z0-9].

import static org.testing.AssertJUnit.assertEquals ;

public class TinyURL {
	private static final String ALPHABET_MAP = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ;
	private static final int BASE = ALPHABET_MAP.length() ;

	public static String encode ( int IndexNum ) {
		StringBuilder sb = new StringBuilder() ;
		while ( IndexNum > 0 ) {
			sb.append ( ALPHABET_MAP.charAt ( IndexNum % BASE ) ) ;
			IndexNum /= BASE ;
		return sb.reverse().toString() ;

	public static int decode ( String str ) {
		int Num = 0 ;

		for ( int i = 0, len = str.length(); i < len; i++ ) {
			Num = Num * BASE + ALPHABET_MAP.indexOf ( str.charAt(i) ) ;
		return Num ;

	public static void main ( String[] args ) {
		//System.out.println ( "Encoding for 123 is " + encode(123) ) ;
		//System.out.println ( "Decoding for b9 is " + decode ("b9" ) ) ;

		assertEquals ( "b9", encode(123) ) ;
		assertEquals ( 123, decode("b9") ) ; 

- R@M3$H.N January 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
of 2 vote

Nice Solution...

- Sarath_P January 16, 2015 | Flag Reply

Add a Comment

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.


is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More


CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More