Challenge - 5 Problems
Iterator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple Iterator implementation
What is the output of this PHP code that implements Iterator to loop over an array of colors?
PHP
<?php class ColorIterator implements Iterator { private array $colors; private int $position = 0; public function __construct(array $colors) { $this->colors = $colors; } public function current(): string { return $this->colors[$this->position]; } public function key(): int { return $this->position; } public function next(): void { $this->position++; } public function rewind(): void { $this->position = 0; } public function valid(): bool { return isset($this->colors[$this->position]); } } $colors = ['red', 'green', 'blue']; $iterator = new ColorIterator($colors); foreach ($iterator as $key => $color) { echo "$key:$color "; } ?>
Attempts:
2 left
💡 Hint
Remember the Iterator interface methods control the keys and values during foreach.
✗ Incorrect
The Iterator interface methods current(), key(), next(), rewind(), and valid() control the iteration. The code outputs keys 0,1,2 with their colors separated by spaces.
🧠 Conceptual
intermediate1:30remaining
Understanding valid() method in Iterator
What is the role of the valid() method in a PHP Iterator implementation?
Attempts:
2 left
💡 Hint
Think about how foreach knows when to stop looping.
✗ Incorrect
valid() returns true if the current position is valid, so foreach continues looping. If false, iteration stops.
🔧 Debug
advanced2:00remaining
Identify the error in this Iterator implementation
What error will this PHP code produce when trying to iterate over the object?
PHP
<?php class NumberIterator implements Iterator { private array $numbers = [1, 2, 3]; private int $pos = 0; public function current() { return $this->numbers[$this->pos]; } public function key() { return $this->pos; } public function next() { $this->pos++; } public function rewind() { $this->pos = 0; } // Missing valid() method } $iter = new NumberIterator(); foreach ($iter as $num) { echo $num . ' '; } ?>
Attempts:
2 left
💡 Hint
Check if all required Iterator methods are implemented.
✗ Incorrect
The Iterator interface requires valid() method. Missing it causes a fatal error.
📝 Syntax
advanced1:30remaining
Which option correctly implements the rewind() method?
Choose the correct rewind() method implementation for a PHP Iterator that resets position to zero.
Attempts:
2 left
💡 Hint
rewind() should reset the iterator to the start.
✗ Incorrect
rewind() must reset the internal pointer to the first element, usually by setting position to 0.
🚀 Application
expert3:00remaining
Custom Iterator to iterate only even numbers
Given this PHP Iterator class, what is the output when iterating over it?
PHP
<?php class EvenNumberIterator implements Iterator { private array $numbers; private int $position = 0; public function __construct(array $numbers) { $this->numbers = $numbers; $this->rewind(); } public function current(): int { return $this->numbers[$this->position]; } public function key(): int { return $this->position; } public function next(): void { do { $this->position++; } while (isset($this->numbers[$this->position]) && $this->numbers[$this->position] % 2 !== 0); } public function rewind(): void { $this->position = 0; if (isset($this->numbers[$this->position]) && $this->numbers[$this->position] % 2 !== 0) { $this->next(); } } public function valid(): bool { return isset($this->numbers[$this->position]); } } $nums = [1, 2, 3, 4, 5, 6]; $iter = new EvenNumberIterator($nums); foreach ($iter as $key => $val) { echo "$key:$val "; } ?>
Attempts:
2 left
💡 Hint
The iterator skips odd numbers by advancing position until an even number is found.
✗ Incorrect
The iterator starts at position 0 (value 1, odd), calls next() to skip to position 1 (value 2, even). Then next() skips odd numbers similarly. Output keys are original positions of even numbers.