Reconstruct a permutation of 0 to n from an increase/decrease string

Quick Overview

Given a string of I and D characters, build any permutation of the integers 0 to n whose adjacent pairs rise and fall the way the string says. It tests a greedy two-pointer construction, a short correctness argument, and linear-time reasoning.

Reconstruct a permutation of 0 to n from an increase/decrease string

Company: Airbnb

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

A permutation `perm` of the `n + 1` integers in the range `[0, n]` can be represented as a string `s` of length `n` where: - `s[i] == 'I'` if `perm[i] < perm[i + 1]`, and - `s[i] == 'D'` if `perm[i] > perm[i + 1]`. Given a string `s`, reconstruct the permutation `perm` and return it. If there are multiple valid permutations, return any of them. For example: ```text Input: s = "IDID" Output: [0,4,1,3,2] ``` One valid permutation is `[0, 4, 1, 3, 2]`: `perm[0] < perm[1]` (I), `perm[1] > perm[2]` (D), `perm[2] < perm[3]` (I), `perm[3] > perm[4]` (D). ```text Input: s = "III" Output: [0,1,2,3] ``` Each consecutive pair is increasing, which matches every `'I'` character. ```hint Where to look For each character, ask which of the values you have not used yet is guaranteed to satisfy the comparison with whatever value ends up at the next position. ``` ### Constraints & Assumptions - `1 <= s.length <= 10^5` - `s[i]` is either `'I'` or `'D'`. - Any permutation that matches `s` is accepted. ### What a Strong Answer Covers - A construction that builds the permutation in one pass over `s`, with no search or backtracking. - An argument for why every `'I'` and `'D'` comparison holds, including the comparison between the last two positions. - Why each value in `[0, n]` is used exactly once, so the result is a permutation. - Time and space complexity. - Edge cases such as a string of length 1, all `'I'`, or all `'D'`. ### Follow-up Questions - How would you return the lexicographically smallest permutation that matches `s`? - How would you count how many permutations match `s`, modulo ${10^9 + 7}$? - How would you write a checker that accepts any valid output, for a test harness?

Overview: Given a string of I and D characters, build any permutation of the integers 0 to n whose adjacent pairs rise and fall the way the string says. It tests a greedy two-pointer construction, a short correctness argument, and linear-time reasoning.

Airbnb logo
Airbnb
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

A permutation perm of the n + 1 integers in the range [0, n] can be represented as a string s of length n where:

  • s[i] == 'I' if perm[i] < perm[i + 1] , and
  • s[i] == 'D' if perm[i] > perm[i + 1] .

Given a string s, reconstruct the permutation perm and return it. If there are multiple valid permutations, return any of them.

For example:

Input: s = "IDID"
Output: [0,4,1,3,2]

One valid permutation is [0, 4, 1, 3, 2]: perm[0] < perm[1] (I), perm[1] > perm[2] (D), perm[2] < perm[3] (I), perm[3] > perm[4] (D).

Input: s = "III"
Output: [0,1,2,3]

Each consecutive pair is increasing, which matches every 'I' character.

Constraints & Assumptions

  • 1 <= s.length <= 10^5
  • s[i] is either 'I' or 'D' .
  • Any permutation that matches s is accepted.

What a Strong Answer Covers Guidance

  • A construction that builds the permutation in one pass over s , with no search or backtracking.
  • An argument for why every 'I' and 'D' comparison holds, including the comparison between the last two positions.
  • Why each value in [0, n] is used exactly once, so the result is a permutation.
  • Time and space complexity.
  • Edge cases such as a string of length 1, all 'I' , or all 'D' .

Follow-up Questions Guidance

  • How would you return the lexicographically smallest permutation that matches s ?
  • How would you count how many permutations match s , modulo 109+7{10^9 + 7} ?
  • How would you write a checker that accepts any valid output, for a test harness?