Report each query's index in a move-to-front permutation

Quick Overview

Process queries on the permutation 1 to m: report the 0-based index of each queried value, then move that value to the front. It tests careful array simulation, the order of reads and updates, and complexity analysis for repeated position lookups.

Report each query's index in a move-to-front permutation

Company: IBM

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You start with the permutation `[1, 2, 3, ..., m]` and receive an array `queries`. Process the queries in order. For each value `queries[i]`, find its 0-based index in the current permutation, record that index, and then move `queries[i]` to the front of the permutation (index 0). Return the array of recorded indices, one per query. ### Function Signature ```python def process_queries(queries: list[int], m: int) -> list[int]: ``` ### Rules - Indices are 0-based. - The index is recorded before the value is moved. - Moving a value to the front shifts every element that was before it one place to the right and leaves the rest of the order unchanged. A value already at index 0 stays where it is. - The output has the same length as `queries`, and entry `i` is the index recorded for `queries[i]`. ### Constraints - `1 <= m <= 1000` - `1 <= len(queries) <= 1000` - `1 <= queries[i] <= m` ### Examples ```text Input: queries = [3, 1, 2, 1], m = 5 Output: [2, 1, 2, 1] ``` The permutation starts as [1, 2, 3, 4, 5]. Query 3 is at index 2 and moves to give [3, 1, 2, 4, 5], then query 1 is at index 1 ([1, 3, 2, 4, 5]), query 2 is at index 2 ([2, 1, 3, 4, 5]), and the last query 1 is at index 1, which restores [1, 2, 3, 4, 5]. ```text Input: queries = [4, 1, 2, 2], m = 4 Output: [3, 1, 2, 0] ``` Starting from [1, 2, 3, 4], the first three moves produce [4, 1, 2, 3], then [1, 4, 2, 3], then [2, 1, 4, 3]. The last query asks for 2 while it is already at the front, so it records 0 and the permutation does not change.

Overview: Process queries on the permutation 1 to m: report the 0-based index of each queried value, then move that value to the front. It tests careful array simulation, the order of reads and updates, and complexity analysis for repeated position lookups.

IBM logo
IBM
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You start with the permutation [1, 2, 3, ..., m] and receive an array queries. Process the queries in order. For each value queries[i], find its 0-based index in the current permutation, record that index, and then move queries[i] to the front of the permutation (index 0). Return the array of recorded indices, one per query.

Function Signature

def process_queries(queries: list[int], m: int) -> list[int]:

Rules

  • Indices are 0-based.
  • The index is recorded before the value is moved.
  • Moving a value to the front shifts every element that was before it one place to the right and leaves the rest of the order unchanged. A value already at index 0 stays where it is.
  • The output has the same length as queries , and entry i is the index recorded for queries[i] .

Constraints

  • 1 <= m <= 1000
  • 1 <= len(queries) <= 1000
  • 1 <= queries[i] <= m

Examples

Input: queries = [3, 1, 2, 1], m = 5
Output: [2, 1, 2, 1]

The permutation starts as [1, 2, 3, 4, 5]. Query 3 is at index 2 and moves to give [3, 1, 2, 4, 5], then query 1 is at index 1 ([1, 3, 2, 4, 5]), query 2 is at index 2 ([2, 1, 3, 4, 5]), and the last query 1 is at index 1, which restores [1, 2, 3, 4, 5].

Input: queries = [4, 1, 2, 2], m = 4
Output: [3, 1, 2, 0]

Starting from [1, 2, 3, 4], the first three moves produce [4, 1, 2, 3], then [1, 4, 2, 3], then [2, 1, 4, 3]. The last query asks for 2 while it is already at the front, so it records 0 and the permutation does not change.