Facebook Interview Question for Software Engineer / Developers






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

while (!haystack.empty())
{
if (haystack.element() == needle)
return true;
else
haystack.removeElement();
}

- Anonymous May 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

lol...haystack aint hay+Data structure wala stack.its haystack!!!!
first of all the above solution is wrong as its O(n) and n=millions in a Haystack.

Possible tools used: a very precise weighing machine that can weigh a needle and a hay.

Logic:
1.Divide the haystack in half by weight
2.Search for needle in the heavier one.

Algo:binary search
Possible test for needle: wieght or magnet

- Steve Rogers May 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you divide a haystack in half by weight, then neither half is heavier ... duh. Put each half on a floating platform -- the one that displaces less water is denser, and contains the needle. But it would be easier to spread the haystack thinly and search by eye, or point a fan at the haystack ...

- ianam May 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Also, it says interviewer was looking for coding skills, so the "haystack" obviously is a data structure -- probably a string. People who cannot accurately and unambiguously describe their interview questions should fail all interviews.

- ianam May 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I meant half by volume,my bad.

- Mr. India May 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assume each needle is unique there can be many way to think about it like haystack can array of needles or linked list of nodes then 2nd thing is that on the basic of what criteria we will find the needle is that
size or type/colour etc..

then if needle are unsorted then we can sort the needle on the basis of size & then search using binary search so time O(nlogn) in case of linked list we will use merge sort thats also has O(nlogn) where n is size of haystack

else if array or linked are sorted then we can find the needle in O(logn) in array & O(n) in linked list

else as its the grayStack so put all the needles into stack & retrieve one by one until retrieve needle is not equals to given needle we checking the needle here based on some type /name/ranking/etc on the basis of which we can compare the needle with others.

while (!haystack.empty())
{
if (haystack.element() == needle)
return true;
else
haystack.removeElement();
}

it can also be done in O(n)

Correct me if anything wrong

- WgpShashank May 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

rajcools if you are not satisfied with above answer you may like it using KMP Algorithm

basically we have to Write a program that finds all occurences of a given pattern in a given input string. This is often referred to as finding a needle in a haystack.

The algorithm has to detect all occurences of the needle in the haystack. It should take the needle and the haystack as input, and output the positions of each occurence, as shown below. The suggested implementation is the KMP algorithm, as, a naive approach will probably exceed the time limit, whereas other algorithms are more complicated...e.g Rabin Karp so The choice is yours.

so if we do pattern matching using KMP algorithm we can find the needle inn O(m+n)
n=length of haystack
m=length of needle

also it depends on how one understand the question & how one approach as i am sure this problem can be solved by many ways but i think KMP is best Suitable for this.

I provide the efficient solution let me know if better one exist

Shashank

- WgpShashank June 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Knuth Moris Pratt Algorithm

- viiids November 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

OMG! Boyer Moore is the only one :D hehe

- mscheliga1980@googlemail.com March 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hashing is easier to code in an interview and works well in practice. KMP is good to mention to convey to the interviewer that one knows about worst case complexities

- just_do_it March 26, 2015 | 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