## Google Interview Question for Software Engineers

Country: United States

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

I feel this question can be solved by stack. once we found previous element in the stack is larger than current element, than pop out

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

ss

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

``````def makeheap( heap, start, N ):
for i in reversed( xrange( start, N/2 +1 ) ):
heapify( heap, i, N )

def heapify( heap, i, last ):
while(1):
l = i*2
r = i*2 +1
if l >= last:
return
if r >= last:
m = l
else:
if heap[l] <= heap[r]:
m = l
else:
m = r
if heap[m] < heap[i]:
heap[m],heap[i] = heap[i],heap[m]
i = m
else:
return

def lexi( arr, k):
n = len(arr )
heap = [ i for i in arr]
makeheap( heap, 0, len(arr))
h = dict()
for ind,i in enumerate(arr):
if i in h:
h[i].append( ind )
else:
h[i] = [ind]

i =0
start = 0
end = n - k
res = ""
last = n-1
c = 0
print h
while c < k:
print heap
index = h[ heap[0] ][0]
print index
if index > start and index < n - k+ c:
res+= heap[0]
c+=1
start = index
del h[heap[0]][0]
heap[0] = heap[last]
last-=1
heapify( heap, 0, last )
return res

print lexi( ["5","1","2","4","12","2"],2)``````

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.

### 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.