## The Ferris Wheel Challenge

I recently encountered a "Ferris Wheel" puzzle that I'd like to share. This is an adapted version, but the principles remain unchanged.

2020/08/18

You're going to meet your friend at the circus. The two of you have decided to meet at the Ferris Wheel attraction. Your friend, having been near the front of the queue when the ride opened, has already boarded and was allowed to reserve a seat for you in her cart.

The wheel has 𝑵 carts with 4 seats each, and each ride lasts for 𝑻 minutes. Since the ride has already commenced for the day, the wheel has been running at full capacity, safe for the seat that is being kept open for you.

When you arrive, there is 𝑿 people queued in front of you. Incidentally, you also noticed that your friend just spun past the boarding point on the Ferris Wheel.

Provided all this information, your program should determine how many people you need to allow to pass you in the queue in order for you to board the same car that your friend is in.

##### Input

A comma-delimited string with three variables: 𝑵, 𝑻, 𝑿

##### Output

The amount of people you need to let pass you in the queue. If you are unable to join your friend in the same cart, the output should be `-1`.

``````const ferris = function (input) {

input = input.split(',');

// 𝑵: carts on wheel
// 𝑿: people ahead in queue
const N = parseInt(input[0], 10);
const X = parseInt(input[2], 10);

// static data points
const seatsInCart = 4;
const seatsReserved = 1;

const seatsTotal = N * seatsInCart;
const seatsAvail = seatsTotal - seatsReserved;
const amountPass = seatsAvail - X;

return (amountPass < 0) ? -1 : amountPass;
}

ferris('9,15,32'); // 3
ferris('12,8,62'); // -1
ferris('30,4,15'); // 104
``````

#### Explanation

The time variable (𝑻) is a red herring, and can be safely disregarded for the purpose of find the answer.

First, we extract and store the relevant data points, 𝑵 and 𝑿. Since upon your arrival your friend were at the boarding point location, we know the wheel has to complete a full rotation before his cart will be ready for you to board again.

We determine the total number of seats available to new passengers, and then subtract the number of people ahead of you in the queue (𝑿) from it. The difference in the result is the number of people you should allow to pass you in the queue in order for you to be first in line when your friend's cart is at the boarding point again.

Credits
Filip MrozVallone Design
Tags
interview algorithm JavaScript engineering