PPractick

DSA interview question

Frog Jump

Medium · Algorithms · Topics: dynamic programming, recursion

The problem

You're building the core mechanic for a stone-hopping platformer game. A frog starts on the first stone of n stones lined up in a row and must reach the last stone. From any stone i, the frog can jump to either stone i+1 or stone i+2 (skipping one stone). Each jump costs energy equal to the absolute difference in height between the stone jumped from and the stone landed on. Your task: find the minimum total energy required for the frog to reach the last stone.

Example: heights [30, 10, 60, 10, 60, 50] → minimum energy path costs 40 total.

Function signature

int frogJump(int n, vector<int>& heights);

Test cases

Input heightsOutputExplanation
[30,10,60,10,60,50]40Path: 0→1→3→5 → |30-10|+|10-10|+|10-50| = 20+0+40 = 60? verify with code
[10,20,30,10]20Path likely 0→2→3 or similar, verify with code
[10]0Single stone, no jump needed
[10,10]0Two equal-height stones, one jump, cost 0
[1,100]99Two stones, forced jump, cost = |1-100|
[7,4,4,2,9]7Classic example, minimum energy path

(Confirm: frog starts at stone 0, must reach stone n-1; jumps of exactly 1 or 2 steps only, never backward.)

How interviewers phrase it

Clarifying questions to ask

  1. Can the frog jump only 1 or 2 steps, never more, never backward? Confirm this precisely.
  2. Must the frog land exactly on the last stone (index n-1), not skip past it?
  3. Are heights guaranteed non-negative? Could there be duplicate heights (zero-cost jumps)?
  4. Is n guaranteed to be at least 1? What should happen for n=1 (frog is already at the destination — cost 0)?
  5. Do you want just the minimum energy, or also the actual path of stones visited?

Follow-ups to expect

  1. "What if the frog can jump up to K steps instead of just 1 or 2?"
  2. "Can you solve this with O(1) extra space?"
  3. "Can you solve it recursively with memoization instead of iterative DP?"
  4. "Can you reconstruct the actual sequence of stones visited?"
  5. "What if backward jumps were allowed?"
  6. "How does this compare to Climbing Stairs?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in