Orderly Queue

HardStringsmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a string s and an integer k. In one move you choose one of the first k characters of s, remove it, and append it to the end of the string. You may make any number of moves (including none). Return the **lexicographically smallest** string that can be produced. Think about which arrangements are reachable for k = 1 versus k >= 2 before writing any search.

Examples

Input: s = "cba", k = 1
Output: "acb"
Rotations of "cba" are "cba", "bac", "acb"; the smallest is "acb".
Input: s = "baaca", k = 3
Output: "aaabc"
With k >= 2 any ordering is reachable, so the answer is the sorted string.

Constraints

  • 1 <= k <= s.length <= 1000
  • s consists of lowercase English letters

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems