Google Interview Question
1,223 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Google:
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (5)
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
2 of 2 people found this helpful
For all substrings, test it to see if it has all the characters. For those that pass, take the shortest. There are O(n^2) substrings, and the test takes O(n) time, so we have O(n^3).
However, we are doing some work over and over. I.e. some substrings are tested, and then retested as parts of other substrings. If we avoid this duplication, we can get down to O(n^2). I'm guessing that's the best we can do.
Helpful Answer?
Yes |
No
Inappropriate?
0 of 2 people found this helpful
Find beg, the first position in A such that A[beg] is in S. Find the smallest value named end such that A[beg], ..., A[end] has all the characters in S. The length of the substring found so far is end - beg + 1 (assuming this operation succeeded).
Increment beg until A[beg] is again in S. Increment end until we again have in the substring all characters in S (we just need to get back the character we lost when we incremented beg). We found another solution.
Keep on incrementing beg and end. Keep track of the shortest solution.
Overall it is an O(n) algorithm. An actual implementation needs to take into account what data structures to use to hold the needed data.
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
0 of 0 people found this helpful
by somebody: