Facebook Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

Since you specifically mention a unix file system, you will probably get bonus points for a one line terminal solution before designing a program.

grep -o -w '\w\{4\}' /usr/share/dict/words | sort --random-sort | head -n 1

or

grep -o -w '\w\{4\}' /usr/share/dict/words | shuf -n 1

would work

- Ryan Latham October 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Assuming the file is sorted with longer length words appearing after shorter one.

Do binary/interpolation search for aaaa and zzzz.

The positions between those two locations are in effect an array of the 4 letter words. (5 bytes each, assuming this is ascii file, with newline after each word)

The positions will tell you how many 4 letter words are there . Say that is W.

Now you can pick a random number in 1 to W. And pick the corresponding word.

This will issue very few reads (and seeks) on the file.

- Anonymous October 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Of course, if the words is not too big, etc, there are probably simpler methods (like scanning the file and storing the 4 letter words), but which perform slower.

- Anonymous October 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

The file: /usr/share/dict/words is never sorted. It's in alphabetical order.

- Anonymous October 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I meant- ... never sorted on the basis of Word Length.

- Anonymous October 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The assumption does NOT hold since dictionary is ordered alphabetically, not by non-decreasing length.

- xiaohui May 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

All these unix command line based ideas are great, but if we're asked not to use these but to think of an algorithm and print it out, would reservoir sampling be a good fit?

- Design.. November 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Maybe. Do not count words of length not equal to 4 and set k to 1. Time complexity O(n), space complexity O(1).

- xiaohui May 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you collect all the words in array, then you can generate random number and pick the word using array[randomnumber].

Looks need more details on this question.

- RR October 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

maximum number of 4letter words can be 26^4=456,976 ,working on a 32 bit or 64 bit domain we can simply produce a garbage value to cover this range and cm with the random word provided we have got a sub array of 4-letter words from the file

- Anonymous October 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sed -n '/^[a-zA-Z]\{4\}$/p' /usr/share/dict/words

They are probably testing your ability to use preexisting tools! If you are reinventing
the wheel then you waste man hours, unproductive.

- Anonymous October 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

str = "/usr/share/dict/words"
  r_str = ""
  while r_str.size < 4 do
    c = str[rand(str.size)]
    r_str += c if c != "/"
  end

;)

- did I make this right? October 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class GenerateRandom {

public static void main(String args[]){
String s="/usr/share/dict/words:";
String word="";
int l=s.length();
List shuffledList= new ArrayList();
for(int i = 0; i < l; i++)
shuffledList.add(s.charAt(i));
Collections.shuffle(shuffledList);
List newList=shuffledList.subList(0, 4);
for(Object i:newList)
word+=i;
System.out.println("The new word is "+word);
}
}

- Anonymous October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It says generate "a 4 letter word" not all. It seems like most of the answers read the whole file and parse the whole file which seems kind of brute force. Here is a python version that reads 3 orders of magnitude less of the file:

#!/usr/bin/python

import random
import os

CHUNK_SIZE  = 1024


def random_word( wlen ):
    wfile = open('/etc/dictionaries-common/words','r')
    flen = os.stat('/etc/dictionaries-common/words').st_size
    chunks = flen / CHUNK_SIZE
    word = None
    # We might have to try a couple times if we get a chunk that does
    # not have any words of our requested length.
    while not word:
        rpos = random.randint(0,chunks) * CHUNK_SIZE        
        wfile.seek(rpos)
        buf = wfile.read(CHUNK_SIZE)
        # We skip the first and last word because the seeks and read
        # may have left partial words on the ends.
        words = [x  for x in buf.split("\n")[1:-1] if len(x) == wlen ]
        if words:
            p = random.randint(0,len(words)-1)
            word = words[p]

    return word


print random_word(4)

- Anonymous July 20, 2014 | Flag Reply


Add a Comment
Name:

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

Books

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

Videos

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