Google | Phone | Order browser tabs from a given index and in given order
Anonymous User
1326

Had a phone screen interview with Google. Was asked following question:

We have list of tabs. All Unique numbers:
[2,4,5,6,7,9,3]

We are given input of index and another list
eg. Index = 2, Order = [7,2,9]

I had to arrange the list with the input list at the given index
eg. [4,5,7,2,9,6,3]

It is assumed that there would always be a right answer

Here is what I suggested:

answer = []
tabs = [2,4,5,6,7,9,3]

Here is roughly the solution I gave:

answer = [] 
tabs = [2,4,5,6,7,9,3]
index = 2
order = [7,2,9]
order_set = set(order)
count = 0

for i,tab in enumerate(tabs):
	if count == index:
        answer = answer + order
    if tab not in order_set:
        answer.append(tab)
        count += 1
print(answer)

n = length of tabs
Time complexity: o(n)
Space Complexity: o(n)

The Interviewer wanted inplace solution. I couldn’t complete it.
He presented me with the following solution:

  1. Find the first element in the Order, and keep swapping until that element reaches end of the tabs list.
  2. Find rest of the elements in the Order and keep swapping until that element reaches end of the tabs list.
  3. By swapping elements move forward the Order elements that now at the end of the list until they reach index.

Which lead to time complexity of: O(n*len(order)*i)

I think my solution was better. But don't know if I would pass.

[UPDATE: Got Rejected]

Comments (10)