Pergunta de entrevista da empresa Bloomberg

1. Implement a queue using stacks 2. Search through an array of integers and find pairs that add to a target

Respostas da entrevista

Sigiloso

13 de nov. de 2014

1. Google question and you'll find actual solution on StackOverflow 2. Have a front and back pointer to compare/add values. If the sum is greater than your target, increment back pointer down. If sum is too small, increment front pointer up. If you find match, output pair and increment both (up and down). When pointers cross over, end search. This only works for sorted arrays (which is what I was given)

Sigiloso

15 de jun. de 2016

public class QueueOnStacks { Stack main = new Stack(); Stack sub = new Stack(); public void offer(int item) { main.push(item); } public Integer poll() { while(!main.isEmpty()) { sub.push(main.pop()); } int result = sub.pop(); while(!sub.isEmpty()) { main.push(sub.pop()); } return result; } public static void main(String[] args) { QueueOnStacks q = new QueueOnStacks(); q.offer(1); q.offer(2); q.offer(3); for(int i = 0; i < 3; i++) { System.out.print(q.poll()+" "); } } }