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.
Be the first one to answer
Add answer anonymously...
Bajaj Electricals Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter