Pergunta de entrevista da empresa LinkedIn

Coding: Create a stack with the usual push() & pop(), but with an additional function getMiddle() that returns the middle element of the stack in constant time.

Respostas da entrevista

Sigiloso

28 de ago. de 2016

import os import re import sys class Stack: def __init__(self): self.arrList = [] def isEmpty(self): if len(self.arrList): return False else: return True def push(self, val): self.arrList.append(val) def pop(self): if not self.isEmpty(): self.arrList[len(self.arrList)-1] self.arrList = self.arrList[:len(self.arrList)-1] else: print "Array list is empty" def returnMiddle(self): if not self.isEmpty(): mid = len(self.arrList)/2 return self.arrList[mid] else: print "Array list is empty" def listStack(self): print self.arrList s = Stack() s.push(5) s.push(6) s.listStack() print s.returnMiddle() s.pop() s.listStack() s.push(20) s.push(45) s.push(435) s.push(35) s.listStack() print s.returnMiddle() s.pop() s.listStack()

Sigiloso

12 de jan. de 2017

public int getNext(int[] ar, int k) { int low = 0; int high = ar.length-1; int mid = low+(high-low)/2; if (ar[high] k && (mid==0 || ar[mid-1]<=k)) { return ar[mid]; } if(ar[mid]<=k) { low = mid+1; } else { high = mid; } mid = low+(high-low)/2; } return -1; }

Sigiloso

20 de abr. de 2016

public class CreateStack { List l = new LinkedList(); public void push(Integer i) { l.add(i); } public void pop(){ l.remove(l.size()-1); } public Integer getMiddle(){ return l.get((l.size()-1)/2); } }