Google Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

Build a Trie Tree for all the strings with reverse order, find the longest common internal node T. Then from root to T, the output is the longest common suffix.

- Cyanny February 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Note that the trie is not even needed. The suffix can be calculated incrementally.

public static String reverse(String s) {
        return new StringBuilder(s).reverse().toString();
    }

    public static String getSuffix(Set<String> words) {
        String suffix = null;
        for (String word : words) {
            word = reverse(word);
            if (suffix == null) {
                suffix = word;
            } else {
                int suffixLength;
                for (suffixLength = 0; suffixLength < Math.min(word.length(), suffix.length()); ++suffixLength) {
                    if (word.charAt(suffixLength) != suffix.charAt(suffixLength)) {
                        break;
                    }
                }
                suffix = suffix.substring(0, suffixLength);
            }
        }
        return reverse(suffix);
    }

- Danstahr March 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Reverse all the strings and find the longest common prefix ?

- Anonymous February 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

1. reversed all words
2. sort these reversed strings
3. from the neighboring two words, find the longest prefix
4. reversed the obtained prefixes and update the results

- Anonymous February 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Oh, I think we'd still build a suffix tree, and mark each node as endOfWord if it is the last character of the string. Then the node sequence that contains all strings with endOfWord marked would be the answer.

- Guy February 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

I thought careercup was really helpful. At least for google. Haven't used glassdoor a lot. But my phone interview question (given grid of A,B,C,D...Z. output arrow sequence for a given word) was in careercup and practicing the questions here gave me a lot of confidence and exposed me to great approaches of solving google interview questions. So, I consider careercup really helpful. I had a phone interview with Twitter last week and one with LinkedIn coming up. I am relying completely on careercup questions. Looking at leetcode and a little bit of glassdoor too because careercup doesnt have a lot of twitter questions.

- bp February 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think for longest common substring, a generalized suffix tree is good enough. The stamp boxes problem was confusing... Not sure what it means. But it sounds like the Knapsack problem? Could you give me an example? For the one with BST, what exactly is x1,x2 and etc in the output you demonstrated x1<x2>x3<x4>x5? The position I am interviewing for is SDE, but the questions should be similar to SET except for coming up with test scenarios I guess.

- Guy February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Why would we want to delete all nodes that are not being traversed and all successors? I don't follow here. May I ask what other two questions they asked you?

- Guy February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
-2
of 2 votes

Do you think careercup is more helpful or glassdoor?

- Guy February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think for longest common substring, a generalized suffix tree is good enough. The stamp boxes problem was confusing... Not sure what it means. But it sounds like the Knapsack problem? Could you give me an example? For the one with BST, what exactly is x1,x2 and etc in the output you demonstrated x1<x2>x3<x4>x5? The position I am interviewing for is SDE, but the questions should be similar to SET except for coming up with test scenarios I guess.

- Guy February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Yeah it's (almost like) the knapsack problem
For instance, the stamp denominations are -
7
11
15

If we want stamps amounting to 25 - We do 11 + 15 = 26 instead of 11 + 11 = 24 or 15 + 15=30
If we want stamps amounting to 20 - We do 7 + 7 + 7 = 21 instead of 7 + 15 = 22 or 11 + 11 = 22 or 11 + 7 = 18

x1, x2 etc are values of nodes in the BST. Basically, just print values of all nodes in the BST in that form. Does that make sense?

- bp February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

x1, x2 etc are values of nodes in the BST. So just do in-order traversal with < and > at the right place? Did you actually get asked the same question posted in here?

- Guy February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Yes, it may be simple, but this stamp problem and the x1, x2,.. problem were asked in my interviews. 100% certain about that :). I have hazy memories of other questions so I did not mention them here, but swear to god these are exactly what were asked. The x1,x2 one was not the only question in its interview, but the knapsack problem was the only question in its interview. There were testing questions after the coding for the knapsack one.

- bp February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I am interviewing with amazon and google again very soon. Hope I won't get owned... You seem very knowledgeable. Should not be a problem.

- Guy February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

wait, so if the BST looks like
5
/ \
3 7
/ \ /\
1 4 6 8
The output is 1<3>4<5>6<7>8 ?

- Guy February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I really doubt you will get owned. You seem like a very strong dev. In my 5 years at Microsoft, I have worked with some great developers and I can sense it when someone is good. You should be able to own them :). All the best! And thank you for your kind words.

- bp February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Actually, this

8 1 7 3 6 4 5

I remember solving it by putting equal(or differing by one) number of elements into 2 queues and popping them one by one.

so for your example, my 2 queues were

1 3 4 5
8 7 6

pop them alternately. The second array can also be 6 7 8 depending on the way you traverse. so the popping would be from end in that case. This will work for unbalanced trees with equal number of nodes(or just 1 different) in each queue.

I think that's how I did it.

- bp February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Hey wait, i think you can just use one array containing the inorder traversal of the bst and pop from each end at a time. wth. i think i just over complicated it in that solution.

- bp February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Yeah, these questions were not very difficult. You can easily do these. It happens to me sometimes where I get asked an interview question and I overestimate the difficulty and complexity of the question. That's how I come up with non trivial solutions for trivial questions. I am trying to overcome that.

- bp February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Yeah, that is what I would do too. Run two in-order traversals one in ascending order and one in descending order and put each node on a queue. Then pop one node at a time until reach to the root. But what if the left subtree and right subtree do not have the same height?

- Guy February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think a simpler approach would be to just store the in-order traversal of the whole tree in one single array and pop one at a time from each side of the array.

I did the 2 array approach in the interview though. Not sure why I did that when I could have used just one array. May be I overestimated the question and tried to do something complicated. :)

But like you asked, when the tree is unbalanced, you will get one array smaller than the other. You just pop elements from the smaller to the larger array until they are even in size.

- bp February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@bp Actually, why would the output be 8 < 1 > 7 < 3 > 6 < 4 > 5 ? All the < and > don't seem to make sense.

- Guy February 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Shouldn't it be like 1<4>3<5>7<8>6 ?

- Guy February 24, 2014 | Flag


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