Problem: Ninja's Robot
The Ninja has a robot which navigates an infinite number line starting at position 0 with an initial speed of +1. The robot follows a set of instructions which includes ‘A’ (Accelerate) and ‘R’ (Reverse) to alter its movement.
The robot reacts as follows:
- If instructed with “A”, the robot updates:
position += speed
andspeed *= 2
. - If instructed with “R”, the robot changes direction: if
speed
is positive thenspeed = -1
, otherwisespeed = +1
. Theposition
remains identical.
Task:
For a given positive integer ‘TARGET’, determine and return the minimum length of the instruction sequence required for the robot to reach ‘TARGET’.
Example:
Input:
T = 2
TARGET = 3
TARGET = 6
Output:
2
5
Explanation:
- For the first test case with TARGET = 3, a possible shortest sequence is “AA” which leads the robot to position 3.
- For the second test case with TARGET = 6, a possible shortest sequence is “AAAAA” which leads the robot to position 6.
Constraints:
1 ≤ T ≤ 50
1 ≤ TARGET ≤ 10000
Note that the robot is capable of reaching negative positions as well.

AnswerBot
4mo
Determine the minimum length of instruction sequence for a robot to reach a given target on an infinite number line.
Start at position 0 with speed +1, update position and speed based on 'A' and 'R' in...read more
Help your peers!
Add answer anonymously...
LimeRoad Full Stack Developer interview questions & answers
A Full Stack Developer was asked Q. Valid Parentheses Problem Statement Given a string 'STR' consisting solely of th...read more
A Full Stack Developer was asked Q. Problem: Ninja's Robot The Ninja has a robot which navigates an infinite number ...read more
A Full Stack Developer was asked Q. Group Anagrams Problem Statement Given an array or list of strings called inputS...read more
Popular interview questions of Full Stack Developer
A Full Stack Developer was asked Q1. Valid Parentheses Problem Statement Given a string 'STR' consisting solely of th...read more
A Full Stack Developer was asked Q2. Problem: Ninja's Robot The Ninja has a robot which navigates an infinite number ...read more
A Full Stack Developer was asked Q3. Group Anagrams Problem Statement Given an array or list of strings called inputS...read more
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

