Coinbase | phone-screen | implement-iterators

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2022-03-08Added By Anonymous
Source: Link

How to solve this question?


Design custom Iterator

We are building a page that pulls posts from multiple different sources and displays them in a unified feed. You are going to build a function that sorts these posts in a fair order so each source gets an equal share of screen time. Implement a function that takes a list of lists of integers and returns a single list of integers back. The response should take the first element from each list and add it to the resulting list before moving onto the second one, and so on. As lists are exhausted, they should be skipped over. For example, [[1,2,3],[4,5],[6],[],[7,8,9]] -> [1,4,6,7,2,5,8,3,9]


We now want to start consuming data from sources that never run out of content (like Facebook and Twitter). Our existing implementation would never complete because we couldn’t hold the lists in memory.


We are going to try and change our approach a bit. Build an iterator interface that supports a int next() function that returns the next value in the iterator


and shifts it forward and a bool hasNext() function that lets you know if there is a value left in the iterator. If hasNext() returns true, next() must return a value.


Iterators:


Custom Iterator

Odd, even having infinite stream (Even takes odd as dependency)

Negative Iterator( takes a dependency iterator)

InterLeavingIterator (takes a list of iterators)

Limit Iterator (takes a limit and terminates the infinite iterator)

Comments