Facebook Interview Question for Software Engineer / Developers

Country: United States
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
of 1 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 // bit.ly / 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 October 02, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
of 0 vote

The things that come to my mind while designing a URL shortener service is -
1. First the functional part, we need to design a system which can shorten a given url into some specified length. Generally the purpose of url shortening is to send url in mobile smses which have a fixed length.
2. Scalability? Do we want system to be distributed
3. Do we expect short urls to expire and to be re-used.

To start with a basic design, I can generate a hash of the url, check if the hashed string already present in some distributed DB. We are book-keeping our records. If it is present then return otherwise hash it again. To access the short-url for same string again and again, we can keep a inverse index.

- A September 27, 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