Skip to content

Instantly share code, notes, and snippets.

@apetresc
Last active December 26, 2015 08:29
Show Gist options
  • Save apetresc/7123097 to your computer and use it in GitHub Desktop.
Save apetresc/7123097 to your computer and use it in GitHub Desktop.
Just your typical interview problem template :)
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* An iterator that flattens an iterator of iterators.
*
* Iterator SQUARED!
*/
public class IteratorIterator<T> implements Iterator<T> {
public <T> IteratorIterator(Iterable<? extends T>... iterators) {
/*
* I guess we should really be doing something with all these
* here iterators...
*/
}
@Override
public boolean hasNext() {
/*
* Should return false if and only if we've returned everything
* up to and including the last element of the last non-empty
* iterator.
*/
return false;
}
@Override
public T next() {
/*
* Return the current element and iterate the cursor to the next
* element of the first remaining non-empty iterator.
*
* You can safely assume that next() will NEVER be called if
* hasNext() would return false.
*/
return null;
}
@Override
public void remove() {
// DON'T WORRY ABOUT THIS ONE!!!
}
/**
* The below is meant to be a test case for your implementation above. If done correctly,
* it should print out the digits from 1 to 10, one per line, without any blank lines.
*/
public static void main(String... args) {
List<Integer> a = Arrays.asList(new Integer[] { });
List<Integer> b = Arrays.asList(new Integer[] {1, 2, 3});
List<Integer> c = Arrays.asList(new Integer[] { });
List<Integer> d = Arrays.asList(new Integer[] { });
List<Integer> e = Arrays.asList(new Integer[] {4});
List<Integer> f = Arrays.asList(new Integer[] { });
List<Integer> g = Arrays.asList(new Integer[] {5, 6, 7, 8, 9, 10});
List<Integer> h = Arrays.asList(new Integer[] { });
IteratorIterator<Integer> it = new IteratorIterator<Integer>(a, b, c, d, e, f, g, h);
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("Done!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment