
Asked in LimeRoad
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...
Top Full Stack Developer Interview Questions Asked at LimeRoad
Q. Valid Parentheses Problem Statement Given a string 'STR' consisting solely of th...read more
Q. Group Anagrams Problem Statement Given an array or list of strings called inputS...read more
Q. Buy and Sell Stock - III Problem Statement Given an array prices where the ith e...read more
Interview Questions Asked to Full Stack Developer at Other Companies
Top Skill-Based Questions for LimeRoad Full Stack Developer
Web Development Interview Questions and Answers
250 Questions
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
JavaScript Interview Questions and Answers
250 Questions
Java Interview Questions and Answers
250 Questions
Software Development Interview Questions and Answers
250 Questions
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

