Reverse String Operations Problem Statement

You are provided with a string S and an array of integers A of size M. Your task is to perform M operations on the string as specified by the indices in array A.

The operations should be executed in the sequence they are listed in array A. During the ith operation, you reverse the substring of S starting from position A[i] to len(S) - A[i] - 1 (0-based index).

Your goal is to determine the string after executing all operations.

Example:

Input:
'S' = "aabcd", 'M' = 2, 'A' = [0, 1]
Output:
"dabca"
Explanation:

After the 1st operation (reverse from [0, 4]), 'S' becomes "dcbaa". After the 2nd operation (reverse from [1, 3]), 'S' becomes "dabca". Hence, the resulting string is "dabca".

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ len(S) ≤ 105
  • 1 ≤ M ≤ 105
  • Each A[i] guarantees the range [A[i], len(S) - A[i] - 1] is non-empty.
  • The sum of lengths of S and M is ≤ 105 for all test cases combined.
  • Time limit: 1 sec
AnswerBot
4mo

Perform a series of reverse string operations on a given string based on specified indices.

  • Iterate through the array of indices and reverse the substring of the string based on the given indices.

  • Ensur...read more

Prince Kumar
3mo
let str='aabcd' let arr=[0,1]; let length=arr.length; ///reverse will be of arr[i] to len(str) - arr[i] - 1 let reverseStr=str; for(var i=0;i<arr.length;i++){ let startStr=reverseStr.slice(0,arr[i]) l...read more
Help your peers!
Select
Add answer anonymously...
Tech Mahindra SDE-2 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