String Transformation Problem

Given a string (STR) of length N, you are tasked to create a new string through the following method:

Select the smallest character from the first K characters of STR, remove it from STR, and append it to the new string. Continue this operation until STR becomes empty.

Input:

The first line contains an integer 'T', the number of test cases. Each subsequent line for a test case contains a string (STR) followed by an integer (K) separated by a space.

Output:

For each test case, output the newly formed string after performing the operations. Print each result on a new line.

Example:

Input:
edcba 4
Output:
bacde

Explanation:

For STR = "edcba" and K = 4:
- Initial new string is empty, newString = "".
- First set of characters: ('e', 'd', 'c', 'b'), smallest is 'b', so newString = "b".
- Next set after removal: ('e', 'd', 'c', 'a'), smallest is 'a', so newString = "ba".
- Remaining 'edc', which is appended in sorted order, resulting in newString = "bacde".

Constraints:

  • 1 ≤ T ≤ 100
  • 0 ≤ N ≤ 105
  • 1 ≤ K ≤ 105

Note that the time limit for processing all test cases is 1 second.

AnswerBot
4mo

Given a string and an integer, create a new string by selecting the smallest character from the first K characters of the input string and repeating the process until the input string is empty.

  • Iterate...read more

Help your peers!
Select
Add answer anonymously...

Optum Global Solutions Associate Software Engineer interview questions & answers

An Associate Software Engineer was asked 12mo agoQ. How will you approach a problem which you are completely unaware of?
An Associate Software Engineer was asked Q. Given a string s, reverse the order of words in the string.
An Associate Software Engineer was asked Q. Given the head of a singly linked list, return the middle node of the linked lis...read more

Popular interview questions of Associate Software Engineer

An Associate Software Engineer was asked 11mo agoQ1. How will you approach a problem which you are completely unaware of?
An Associate Software Engineer was asked Q2. Given a string s, reverse the order of words in the string.
An Associate Software Engineer was asked Q3. Given the head of a singly linked list, return the middle node of the linked lis...read more
Optum Global Solutions Associate 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