PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to reason about permutation constraints, sequence ordering, and combinatorial construction under digit-uniqueness and numeric-minimization requirements.

  • medium
  • Airbnb
  • Coding & Algorithms
  • Software Engineer

Construct smallest number from I/D pattern

Company: Airbnb

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem You are given a string `pattern` consisting only of the characters: - `'I'` meaning the next digit is **increasing** - `'D'` meaning the next digit is **decreasing** Construct the **numerically smallest** positive integer (as a string) that satisfies the pattern using **distinct digits** from `1` to `9`. Formally, if `pattern` has length `n`, you must output a string `ans` of length `n + 1` such that for every `i`: - if `pattern[i] == 'I'`, then `ans[i] < ans[i+1]` - if `pattern[i] == 'D'`, then `ans[i] > ans[i+1]` Digits cannot repeat, and you may only use digits `'1'` through `'9'`. ## Input - `pattern`: string of length `n` where `1 <= n <= 8` ## Output - The lexicographically/numerically smallest valid number as a string. ## Example - `pattern = "IID"` → output could be `"1243"` (since `1<2<4>3`).

Quick Answer: This question evaluates a candidate's ability to reason about permutation constraints, sequence ordering, and combinatorial construction under digit-uniqueness and numeric-minimization requirements.

Part 1: Bank Ledger Processor

You are given the starting balances of several bank accounts and a list of operations to process in order. Supported operations are ('deposit', account, amount), ('withdraw', account, amount), ('transaction', from_account, to_account, amount), and ('balance', account). A deposit or withdrawal changes one account. A transaction transfers money from one account to another. Any invalid operation returns -1 and must not change the ledger. An operation is invalid if an account does not exist, the amount is not positive, a withdrawal or transaction would overdraw the source account, or a transaction uses the same source and destination account. Return a list of results in order. For deposit, withdraw, and balance, append the resulting balance as an integer. For transaction, append a tuple (new_from_balance, new_to_balance).

Constraints

  • 0 <= number of accounts <= 10^4
  • 0 <= len(operations) <= 10^5
  • All balances and amounts are integers
  • Valid amounts must be > 0

Examples

Input: ({'A': 100, 'B': 50}, [('deposit', 'A', 20), ('withdraw', 'B', 10), ('transaction', 'A', 'B', 30), ('balance', 'A'), ('balance', 'B')])

Expected Output: [120, 40, (90, 70), 90, 70]

Explanation: Processes all four operation types successfully.

Input: ({'A': 10}, [('withdraw', 'A', 15), ('deposit', 'C', 5), ('transaction', 'A', 'A', 1), ('balance', 'C')])

Expected Output: [-1, -1, -1, -1]

Explanation: Insufficient funds, missing account, self-transfer, and querying a missing account are all invalid.

Input: ({'X': 0}, [('deposit', 'X', 1), ('withdraw', 'X', 1), ('balance', 'X')])

Expected Output: [1, 0, 0]

Explanation: Shows a zero starting balance and a full round trip back to zero.

Input: ({}, [])

Expected Output: []

Explanation: Edge case with no accounts and no operations.

Hints

  1. A hash map from account ID to current balance lets each operation run in constant time.
  2. For invalid operations, be careful not to modify any balance before all checks pass.

Part 2: Minimum Operations in the Water Jug Problem

You have two jugs with capacities jug1 and jug2 liters. Both start empty. In one operation, you may fill a jug completely, empty a jug completely, or pour water from one jug into the other until the source is empty or the destination is full. Return the minimum number of operations needed to end with exactly target liters in either jug. If it is impossible, return -1.

Constraints

  • 0 <= jug1, jug2 <= 100
  • 0 <= target <= 100
  • The target must appear in one jug exactly, not just as the combined total
  • The starting state is always (0, 0)

Examples

Input: (3, 5, 4)

Expected Output: 6

Explanation: One shortest path is fill 5, pour to 3, empty 3, pour to 3, fill 5, pour to 3.

Input: (2, 6, 5)

Expected Output: -1

Explanation: It is impossible to get exactly 5 liters in either jug.

Input: (1, 1, 0)

Expected Output: 0

Explanation: Edge case: target 0 is already satisfied at the start.

Input: (0, 5, 5)

Expected Output: 1

Explanation: Fill the 5-liter jug once.

Hints

  1. Treat each state (a, b) as a node in a graph, where a and b are the current amounts in the two jugs.
  2. Because every operation has equal cost, breadth-first search gives the shortest number of steps.

Part 3: Construct the Smallest Number from an I/D Pattern

You are given a pattern string consisting only of 'I' and 'D'. The pattern length is n, and you must use each digit from 1 to n + 1 exactly once to form the smallest possible number that follows the pattern. 'I' means the next digit must be larger, and 'D' means the next digit must be smaller. Return the resulting number as a string.

Constraints

  • 0 <= len(pattern) <= 8
  • pattern contains only 'I' and 'D'
  • Use digits 1 through len(pattern) + 1 exactly once

Examples

Input: 'IIDDD'

Expected Output: '126543'

Explanation: This is the smallest arrangement of digits 1 through 6 that matches the pattern.

Input: 'D'

Expected Output: '21'

Explanation: A single decreasing relation requires the smallest descending pair.

Input: ''

Expected Output: '1'

Explanation: Edge case: no relations means the smallest available single digit.

Input: 'IDID'

Expected Output: '13254'

Explanation: The stack-based construction yields the lexicographically smallest valid result.

Hints

  1. A run of consecutive 'D' characters forces a descending block in the answer.
  2. Try pushing digits to a stack and flushing the stack whenever you see an 'I' or reach the end.

Part 4: Group Chat Unread Counter

Implement a simplified group chat system. Process operations in order and return one result per operation. Supported operations are: ('create', group_id, users) to create a group with the given list of unique users; ('add', group_id, user) to add a user with unread count 0; ('remove', group_id, user) to remove a user and discard their unread count; ('send', group_id, sender) to send a message from a current member and increase unread counts of every other current member by 1; ('read', group_id, user, k) to mark up to k unread messages as read and return the remaining unread count; and ('unread', group_id, user) to query the unread count. Any invalid operation returns -1 and must not change the system state. Creating a group with duplicate users is invalid. Creating an empty group is allowed.

Constraints

  • 0 <= len(operations) <= 10^4
  • Group IDs and user IDs are strings
  • Total users stored across all groups is at most 10^4
  • For read operations, k is an integer and must be >= 0 to be valid

Examples

Input: [('create', 'g1', ['A', 'B', 'C']), ('send', 'g1', 'A'), ('unread', 'g1', 'B'), ('read', 'g1', 'B', 1), ('add', 'g1', 'D'), ('send', 'g1', 'C'), ('unread', 'g1', 'D')]

Expected Output: [3, 2, 1, 0, 4, 3, 1]

Explanation: Shows creation, sending, reading, adding a user, and unread queries.

Input: [('send', 'g1', 'A'), ('create', 'g1', ['A', 'A']), ('create', 'g1', []), ('unread', 'g1', 'A')]

Expected Output: [-1, -1, 0, -1]

Explanation: Edge cases: sending to a missing group, duplicate users on creation, creating an empty group, and querying a non-member.

Input: [('create', 'g', ['A', 'B']), ('send', 'g', 'A'), ('remove', 'g', 'B'), ('add', 'g', 'B'), ('unread', 'g', 'B')]

Expected Output: [2, 1, 1, 2, 0]

Explanation: Unread state is discarded when a user leaves and resets to 0 on rejoin.

Input: [('create', 'solo', ['U']), ('send', 'solo', 'U'), ('read', 'solo', 'U', 5), ('unread', 'solo', 'U')]

Expected Output: [1, 0, 0, 0]

Explanation: A one-person group can send messages, but no one else receives them.

Hints

  1. Use one hash map to store each group's current members and another to store unread counts per user.
  2. When a user leaves and later rejoins, their unread count should restart at 0.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Count Candies from Unlockable Boxes - Airbnb (medium)
  • Find Optimal Property Combination - Airbnb (medium)
  • Determine Exact Layover Booking - Airbnb (medium)
  • Solve Linked-List and Iterator Problems - Airbnb
  • Implement Text Layout and Query Parsing - Airbnb (easy)