String Rotation Problem Statement

You are given a string named str and an integer D. Your task is to perform both left (anticlockwise) and right (clockwise) rotations on the given string by D units, starting from the initial index. You must return the modified string after performing each rotation.

Input:

The first line of input consists of an integer T, indicating the number of test cases or queries to be executed. 
Each test case consists of two lines:
1. A string str.
2. An integer D, denoting the number of positions by which the string is to be rotated.

Output:

For each test case, output the result of the left rotation and the right rotation of the string, separated by a single space. Each test case result should be on a new line. 

Example:

Left-Right Rotation

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ |str| ≤ 10^5
  • 1 ≤ D ≤ 10^5
  • Where |str| represents the length of the string str.
  • Time Limit: 1 second.

Note:

You do not need to print the results; it is already managed for you. Focus on implementing the following functions: 
1. leftRotate(): This function should return the string after applying a left rotation.
2. rightRotate(): This function should return the string after applying a right rotation.
AnswerBot
4mo

Implement functions to perform left and right rotations on a given string by a specified number of positions.

  • Implement a function leftRotate() to perform left rotation on the string by D units.

  • Impleme...read more

Help your peers!
Select
Add answer anonymously...
Bajaj Electricals Software 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