Frog Jump
Updated
Problem statement
A frog wants to cross a river by hopping from stone to stone. You are given an integer array stones with the positions of the stones, in units from the near bank, sorted in strictly increasing order. stones[0] is always 0, and the frog starts on that stone.
The frog only jumps forward and must land on a stone, never in the water. Its first jump covers exactly 1 unit. If its last jump covered k units, its next jump must cover k - 1, k or k + 1 units, and every jump covers at least 1 unit.
Return true if the frog can reach the last stone, or false if it cannot.
Function
canCross(stones: int[]) → booleanExamples
Example 1
stones = [0,1,2,3,5]return = trueThe first jump goes from 0 to 1. One way across: jump 1 unit to 2, then 1 unit to 3, then 2 units to 5. The jumps cover 1, 1, 1 and 2 units, and each one differs from the jump before it by at most 1.
Example 2
stones = [0,1,3,4,7]return = falseThe frog must go 0 to 1 to 3, with jumps of 1 and 2 units. From 3 it can jump 1, 2 or 3 units, and only the 1-unit jump lands on a stone, at 4. From 4 the next jump covers 1 or 2 units, so it can reach 5 or 6, and neither is a stone. The stone at 7 is never reached.
Example 3
stones = [0,2]return = falseThe first jump must cover exactly 1 unit, which lands in the water at position 1.
Constraints
2 <= stones.length <= 20000 <= stones[i] <= 231 - 1stones[0] == 0stonesis sorted in strictly increasing order.
More Zillow Phone Screen questions
- Valid ParenthesesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
- Time Based Key-Value StorePHONE SCREEN · Seen Feb 2024
- Filter Formatted Records by CriteriaPHONE SCREEN · Seen Sep 2023
- Build a Hash TablePHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2023
- Jump GamePHONE SCREEN · Seen Dec 2022
- Reversible Run-Length EncodingPHONE SCREEN · Seen Nov 2022
- Spiral MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2022
- Two Sum: All PairsPHONE SCREEN · Seen Nov 2022
- Employee Free TimePHONE SCREEN · Seen Oct 2022
- First Unique Character in a StringPHONE SCREEN · Seen Oct 2022
Similar questions at other companies
- Word Break IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Count Ordered Combination Sums with Negative ValuesPinterest · PHONE SCREENPinterest interview questions
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Predicate Expression Tree EvaluatorMongoDB · PHONE SCREENMongoDB interview questions
- Find Max Length of SubsequenceGoogle · PHONE SCREENGoogle interview questions
- Longest Fibonacci-Like SubsequenceAdobe · PHONE SCREENAdobe interview questions
- Longest Common Contiguous Browser HistoryRoblox · PHONE SCREENRoblox interview questions
- Longest Common URL History RunDatadog · PHONE SCREENDatadog interview questions
- Optimize Discount Selection Under a Point BudgetPalantir · OAPalantir interview questions
- Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions