FastPrepLexicographically Smallest Task Queue
Problems/Lexicographically Smallest Task Queue

Lexicographically Smallest Task Queue

Agoda logoAgoda● MediumCONTRACTOA

Updated Jul 2025

QuestionFunctionExamplesConstraints

Problem statement

A system manages a queue of tasks represented by a string taskQueue. Each character is one of:

  • '1': low priority
  • '2': medium priority
  • '3': high priority

You may perform adjacent swaps any number of times, but only for these unordered adjacent pairs:

  1. swap adjacent '1' and '2' tasks
  2. swap adjacent '2' and '3' tasks

You cannot directly swap adjacent '1' and '3' tasks. Therefore, after removing all '2' tasks from the string, the relative order of the remaining '1' and '3' tasks is invariant. Return the lexicographically smallest task order that can be obtained under these swap rules. Do not assume the answer is always the fully sorted string.

A task order a is lexicographically smaller than task order b if, at the first position where they differ, a has the smaller priority character.

Function

getSmallestTaskQueue(taskQueue: String) → String

Examples

Example 1

taskQueue = "13212"return = "12231"

The '2' tasks can move across both '1' and '3' tasks, but '1' and '3' cannot swap directly. The non-'2' subsequence in "13212" is "131", and that relative order must remain. The smallest reachable placement of the two '2' tasks gives "12231".

Constraints

  • 1 <= taskQueue.length <= 2 * 10^5
  • taskQueue contains only '1', '2', and '3'.

More Agoda Online Assessment questions

  • Special Diameter EndpointsOA · Seen Nov 2025
  • TV Series in a Production WindowOA · Seen Nov 2025
  • Count One Groups by SizeOA · Seen Jul 2025
  • Minimum Price With Discount CouponsOA · Seen Jul 2025
  • Highest-Ranked University by CountryOA · Seen Jun 2025
  • Jump Game with Prime-3 StepsOA · Seen Jun 2025
  • Team FormationOA · Seen Apr 2025
  • Two-Core Process AssignmentOA · Seen Apr 2025
  • Unique Digits in RangeOA · Seen Apr 2025
  • Maximize Sum of Processed TimesOA · Seen Sep 2024

Similar questions at other companies

  • Maximum String OperationsMicrosoft · OAMicrosoft interview questions
  • Minimum No-Repeat Segments After One Character RemovalSalesforce · OASalesforce interview questions
  • Longest Non-Decreasing Substring After Pair SwapsVisa · OAVisa interview questions
  • Minimum Deletion Cost for Distinct Adjacent CharactersHSBC · OAHSBC interview questions
  • Minimum Binary Swaps to a PalindromeAT&T · OAAT&T interview questions
  • Robot Assembly Line ConflictsGoogle · OAGoogle interview questions
  • Substring RemovalJP Morgan Chase · OAJP Morgan Chase interview questions
  • Minimum Additions to Make a Valid ABC StringIBM · OAIBM interview questions
  • Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
  • Minimum Adjacent TranspositionsEpic · OAEpic interview questions

Agoda interview experiences

  • Agoda Staff Backend OA Questions: 5 Experiences
All Agoda interview questionsSee Agoda hiring insights
Saved
class Solution {
    public String getSmallestTaskQueue(String taskQueue) {
      // write your code here
    }
}
Checking account…