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 and speed *= 2.
  • If instructed with “R”, the robot changes direction: if speed is positive then speed = -1, otherwise speed = +1. The position 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!
Select
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
LimeRoad Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits