Facebook Interview Question
349 Interview Reviews |
Back to all Facebook Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Facebook:
given a list of words with a same size and a big string that contains one of the permutation of all the words combined(say p), find the startindex of the string p in the big string
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (4)
So let words size be k and there be n words so so size of stream will nk
Now qusetion is how we store these n words I prefer stroing them as in a trie
so we start match stream if it follows the path of word "p" we go along if it doesn't
then we start matching it from kth position of stream, then 2k and so on
Helpful Answer?
Yes |
No
Inappropriate?
1 of 2 people found this helpful
----------------
1. The characters are ascii
2. The string p appears in the big string at the beggining of a word
1. have an array of 255 chars. Find the the frequency of each char in the given set of words. update the array. This code be optimized to be a hash.
2. go through the large string and for each word find the frequency of the characters and see if they match the frequency of the words that was found previously.
There are other optimizations that can be done to the solution. O(length of large string).
Helpful Answer?
Yes |
No
Inappropriate?
import java.util.HashMap;
public class Solution {
public ArrayList<Integer> findStartIndex(String S, String[] L) {
int len = L[0].length();
ArrayList<Integer> ret = new ArrayList<Integer>();
HashMap<String, Integer> count = new HashMap<String, Integer>();
HashMap<String, Integer> found = new HashMap<String, Integer>();
for (String word : L) {
if (!count.containsKey(word)) {
count.put(word, 1);
} else {
count.put(word, count.get(word) + 1);
}
}
loop: for (int i = 0; i <= S.length() - len * L.length; i++) {
String sub = S.substring(i, i + len);
if (!count.containsKey(sub)) {
continue;
}
found.clear();
found.put(sub, 1);
for (int j = 1; j < L.length; j++) {
String next_sub = S.substring(i + len * j, i + len * (j + 1));
if (!count.containsKey(next_sub)) {
continue loop;
}
Integer foundVal = !found.containsKey(next_sub) ? 1 : found.get(next_sub) + 1;
if (foundVal > count.get(next_sub)) {
continue loop;
}
found.put(next_sub, foundVal);
}
ret.add(i);
}
return ret;
}
}
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



0 of 1 people found this helpful
by Interview Candidate: