FunctionRacerProblemsRace

Wheels in the Row

BeginnerRecursionPython, JavaScript, TypeScript, Java, C++

A row of n parked vehicles alternates between motorbikes and cars. The first vehicle is a motorbike with 2 wheels, the second is a car with 4 wheels, the third is a motorbike again, and so on.

Return the number of wheels in the whole row. A loop or a formula works, but try the recursive view: the wheels of n vehicles are the wheels of the first n - 1 vehicles plus the wheels of vehicle number n.

Example 1

n 1
returns 2

One motorbike has 2 wheels.

Example 2

n 2
returns 6

A motorbike and a car: 2 + 4 = 6.

Example 3

n 0
returns 0

An empty row has no wheels.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Recursion problems