codeintuition-logo

Exploring a possible solution


There are two observations to make for the example problem we saw earlier.

  • The first person in the queue knows their position.
  • You could get your position by adding 1 to the position of the person ahead of you.

The two observations above are sufficient conditions for a recursive solution. It is not very difficult to imagine how someone standing in the queue can recursively get their position. They need to ask the person standing ahead of them the same question (about their position) and add 1. Let us examine the entire process between asking and getting the answer.

Liking the course? Check our discounted plans to continue learning.