Bird
0
0
LLDsystem_design~10 mins

Iterator pattern in LLD - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the Iterator interface method.

LLD
interface Iterator {
    boolean [1]();
}
Drag options to blanks, or click blank then click option'
AhasNext
Bnext
Cremove
Dreset
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing next instead of hasNext
Using remove which is optional
2fill in blank
medium

Complete the code to return the next element in the iterator.

LLD
public Object [1]() {
    if (!hasNext()) {
        throw new NoSuchElementException();
    }
    return collection.get(currentIndex++);
}
Drag options to blanks, or click blank then click option'
Aremove
BhasNext
Cnext
Dreset
Attempts:
3 left
💡 Hint
Common Mistakes
Using hasNext instead of next
Trying to remove element here
3fill in blank
hard

Fix the error in the iterator implementation to correctly check for more elements.

LLD
public boolean hasNext() {
    return currentIndex [1] collection.size();
}
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using >= or > which causes errors
Using <= which is off by one
4fill in blank
hard

Fill both blanks to implement a simple iterator for an array.

LLD
class ArrayIterator implements Iterator {
    private Object[] items;
    private int [1] = 0;

    public ArrayIterator(Object[] items) {
        this.items = items;
    }

    public boolean hasNext() {
        return [2] < items.length;
    }
}
Drag options to blanks, or click blank then click option'
AcurrentIndex
Bindex
Cpointer
Dposition
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently
Not initializing the index variable
5fill in blank
hard

Fill all three blanks to complete the iterator's next method.

LLD
public Object [1]() {
    if (!hasNext()) {
        throw new NoSuchElementException();
    }
    return items[[2]++];
}

public void [3]() {
    [2] = 0;
}
Drag options to blanks, or click blank then click option'
Anext
BcurrentIndex
Creset
DhasNext
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing method names
Not incrementing index properly
Missing reset method