Cover image for LinkedIn
Logo

LinkedIn

Part of Microsoft
Engaged employer

LinkedIn

Add an Interview

Interview Question

Senior Software Engineer In Test Interview

-

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.

Interview Answers

3 Answers

0

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()

Harish on

0

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; }

Greeky on

0

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); } }

Sruthi on

Add Answers or Comments

To comment on this, Sign In or Sign Up.