WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Wall Street Quant · Puzzles & Problems · question 24 of 155

A frog can jump over one stone or two at a time. If there are N stones in a line, how many different ways can the frog traverse the path?

📕 Buy this interview preparation book: 155 Wall Street Quant questions & answers — PDF + EPUB for $5

Let’s denote the number of ways to traverse N stones as f(N). We can easily find the first few values:

- If there are 0 stones (N = 0), there’s only one way: just stay at the starting position, so f(0) = 1.

- If there’s only 1 stone (N = 1), the frog can jump over it in only one way, so f(1) = 1.

- If there are 2 stones (N = 2), the frog could either jump over each stone one at a time or jump over both stones at once, so f(2) = 2.

Now, let’s think about the frog’s last jump when traversing N stones. The frog can either land on the second to last stone or the last stone. If the frog’s last jump lands on the second to last stone, it must have jumped over 1 stone, which leaves N - 1 stones to traverse. If the frog’s last jump lands on the last stone, it must have jumped over 2 stones, which leaves N - 2 stones to traverse. This means that:


f(N) = f(N − 1) + f(N − 2)

This recurrence relation is the same as the Fibonacci sequence, shifted by one element:


f(N) = FN + 1

where Fn denotes the nth Fibonacci number. The Fibonacci sequence is defined as F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n > 1. So, to find the number of different ways the frog can traverse N stones, we need to find the (N+1)th Fibonacci number.

Here’s a table with the first few values of N and their corresponding f(N):

 N | f(N)
---|-----
 0 |   1
 1 |   1
 2 |   2
 3 |   3
 4 |   5
 5 |   8
 6 |  13

For example, if there are 4 stones, the frog can traverse the path in 5 different ways:

1. Jump over each stone one at a time (1, 1, 1, 1)

2. Jump over the first two stones, then one at a time (2, 1, 1)

3. Jump over the first stone, then two, then one (1, 2, 1)

4. Jump over the first stone, then one, then two (1, 1, 2)

5. Jump over two stones twice (2, 2)

To calculate f(N) for larger values of N, you can use various algorithms for calculating Fibonacci numbers, such as matrix exponentiation, Binet’s formula, or memoization.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Wall Street Quant interview — then scores it.
📞 Practice Wall Street Quant — free 15 min
📕 Buy this interview preparation book: 155 Wall Street Quant questions & answers — PDF + EPUB for $5

All 155 Wall Street Quant questions · All topics