Home › Learn › World 2: Twin Rivers
FREEPalindrome Ferry
Two pointers from both ends, O(n)
Level 2.2 of Code the Hero, in Twin Rivers: two pointers and sliding windows.
The idea
Compare mirrored positions from the outside in; stop at the first mismatch.
The mission
The ferry only sails if the barrel row reads the same from both banks, like a palindrome. Pip reads from the left, Pix from the right. Compare, then both step inward. One mismatch is enough to say no. If they meet in the middle, the row is a palindrome.
Goal: submit(true) if the row reads the same both ways, otherwise submit(false).
The commands you get
You write real Python, Java or JavaScript. These are the moves the hero understands in this level:
pip.read() / pix.read() | Barrel under each drone |
pip.right() / pix.left() | Move a drone inward |
pip.index / pix.index | Where each drone is |
submit(bool) | Hand in your verdict |
What it costs
| Time | O(n) |
| Extra space | O(1) |
How the stars are earned
- Correct verdict on every test
- At most 2n reads
- At most n reads
Your code is also checked on hidden inputs, so it has to work for any case, not only the one on screen.
No sign-up. Levels open in order, so if this one is still locked you start at the first level that isn't.
More in Twin Rivers
- Row to the Pair: Two pointers on sorted data, O(n)
- Remove Duplicates: Read pointer and write pointer, O(n)
- The Sliding Net: Sliding window, O(n)
- Stretchy Net: Variable sliding window, O(n)
- Container Dock: Two pointers, greedy O(n)
- The Flood: Trapping rain water, two pointers O(n)