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

Given a string and an array of indices, reverse substrings based on the indices to obtain the final string.

  • Iterate through the array of indices and reverse the substrings accordingly

  • Ensure the range s...read more

Help your peers!
Select
Add answer anonymously...

Tredence Senior Software Engineer interview questions & answers

A Senior Software Engineer was asked Q. What is middleware. Write one simple middleware in Express js How we can consume...read more
A Senior Software Engineer was asked Q. 1) Making an api using node & express js 2) Showing data in ui from a server usi...read more
A Senior Software Engineer was asked Q. How do you come out of an iframe in Selenium?

Popular interview questions of Senior Software Engineer

A Senior Software Engineer was asked Q1. What is middleware. Write one simple middleware in Express js How we can consume...read more
A Senior Software Engineer was asked Q2. 1) Making an api using node & express js 2) Showing data in ui from a server usi...read more
A Senior Software Engineer was asked Q3. How do you come out of an iframe in Selenium?
Tredence Senior Software Engineer 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