Grokking the Engineering Manager Coding Interview
Vote

0% completed

​

Zigzag Iterator (medium)

.....

.....

.....

Like the course? Get enrolled and start learning!
A

Anonymous

· 2 years ago

I think hasNext() in this case shouldn't just return true if there is an iterator in the queue, but it should also check if the iterator inside the queue hasNext()

A

Anonymous

· 3 years ago

class Solution { private Queue<Integer> queueFirst; private Solution(){ queueFirst = new LinkedList<>(); } public Solution(List<Integer> v1, List<Integer> v2) { queueFirst = new LinkedList<>(); boolean toggle = true; int v1Index = 0; int v2Index = 0; while(!(v1Index==v1.size()) || !(v2Index==v2.size())){ if(!(v1Index==v1.size())){ queueFirst.add(v1.get(v1Index++)); } if(!(v2Index==v2.size())){ queueFirst.add(v2.get(v2Index++)); } } } public int next() { if(!queueFirst.isEmpty()){ return queueFirst.poll(); } return -1; } public boolean hasNext() { return !queueFirs
Show 1 reply
A

Anonymous

· 3 years ago

What is the benefit of using Queue for this purpose and use additional space - instead of simply having reference of the 2 queues and switching between them?

Show 1 reply

Reading Progress

0%


Vote for new content