0
0
PHPprogramming~20 mins

Iterator interface implementation in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Iterator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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 ";
}
?>
A0:red 1:green 2:blue
Bred green blue
C0:red 1:green
D1:red 2:green 3:blue
Attempts:
2 left
💡 Hint
Remember the Iterator interface methods control the keys and values during foreach.
🧠 Conceptual
intermediate
1:30remaining
Understanding valid() method in Iterator
What is the role of the valid() method in a PHP Iterator implementation?
AIt returns the current element's value.
BIt resets the iterator to the first element.
CIt checks if the current position is valid and iteration should continue.
DIt moves the iterator to the next element.
Attempts:
2 left
💡 Hint
Think about how foreach knows when to stop looping.
🔧 Debug
advanced
2: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 . ' ';
}
?>
AOutputs: 1 2 3
BFatal error: Class must implement method valid() from Iterator interface
CWarning: Undefined offset 3
DNo output, infinite loop
Attempts:
2 left
💡 Hint
Check if all required Iterator methods are implemented.
📝 Syntax
advanced
1:30remaining
Which option correctly implements the rewind() method?
Choose the correct rewind() method implementation for a PHP Iterator that resets position to zero.
Apublic function rewind() { $this->position = 0; }
Bpublic function rewind() { return 0; }
Cpublic function rewind() { $this->position++; }
Dpublic function rewind() { unset($this->position); }
Attempts:
2 left
💡 Hint
rewind() should reset the iterator to the start.
🚀 Application
expert
3: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 ";
}
?>
A2:3 4:5 6:7
B0:1 1:2 2:3 3:4 4:5 5:6
C0:2 1:4 2:6
D1:2 3:4 5:6
Attempts:
2 left
💡 Hint
The iterator skips odd numbers by advancing position until an even number is found.