PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates string manipulation, anagram detection, and constrained dictionary mapping skills, testing attention to character-level constraints and correct disambiguation among similar vocabulary entries.

  • medium
  • Upstart
  • Coding & Algorithms
  • Software Engineer

Decode an anagram sentence using vocabulary constraints

Company: Upstart

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given: - `vocab`: a list of lowercase words (the "dictionary") - `s`: a string containing space-separated scrambled words Each scrambled word in `s` is guaranteed to be an anagram of **exactly one** word in `vocab`, with these extra constraints: - The original word and the scrambled word have the same length. - They share the same first character and the same last character. Note: Some vocabulary words can be anagrams of each other (e.g., `"pears"` and `"spear"`). In that case, the additional constraint (same first/last character) is what makes the mapping unique for each scrambled token. Return the decoded sentence by replacing each scrambled word with its matching vocabulary word, preserving word order and spaces. Example: - `vocab = ["pears", "spear", "something"]` - `s = "seapr sothinmeg"` - Output: `"spear something"` Implement a function: - Input: `string[] vocab, string s` - Output: `string decoded`

Quick Answer: This question evaluates string manipulation, anagram detection, and constrained dictionary mapping skills, testing attention to character-level constraints and correct disambiguation among similar vocabulary entries.

Part 1: Reverse a Number List While Filtering Out Odd Numbers

Given a list of integers nums, return a new list that contains only the even numbers from nums, but in reverse order. Odd numbers must be removed.

Constraints

  • 0 <= len(nums) <= 100000
  • -10^9 <= nums[i] <= 10^9

Examples

Input: ([2, 3, 4],)

Expected Output: [4, 2]

Explanation: The even numbers are 2 and 4, and they appear in reverse order.

Input: ([-3, -2, -1, 0, 5, 8],)

Expected Output: [8, 0, -2]

Explanation: Only -2, 0, and 8 are even, and the result must be reversed.

Input: ([],)

Expected Output: []

Explanation: An empty list has no even numbers.

Input: ([7],)

Expected Output: []

Explanation: The single element is odd, so nothing remains.

Hints

  1. Try iterating from the end of the list instead of reversing first and then filtering.
  2. Remember that negative even numbers also satisfy x % 2 == 0.

Part 2: Decode a Doubly Encrypted Message Using Inferred Cipher Keys

A message from C back to A was encrypted twice. First, the sender applied the same character-substitution rule used for messages from A to B. Then, they applied the same character-substitution rule used for messages from B to C. You are given one known plaintext/ciphertext pair for A to B and one known plaintext/ciphertext pair for B to C. Infer both substitution rules, then decode the final message by reversing the B to C cipher first and the A to B cipher second. Each hop uses a one-to-one per-character substitution. If any sample pair is inconsistent, or if the final message contains a character that cannot be decrypted, return an empty string.

Constraints

  • 0 <= len(clear_msg_ab), len(ciphered_msg_ab), len(clear_msg_bc), len(ciphered_msg_bc), len(ciphered_msg_cba) <= 100000
  • All strings contain lowercase English letters only
  • Within each sample pair, the same plaintext character must always map to the same ciphertext character, and vice versa

Examples

Input: ('abcd', 'mnop', 'mnop', 'xyzw', 'yxwz')

Expected Output: 'badc'

Explanation: Decrypt yxwz with the inverse of B to C to get nmpo, then decrypt nmpo with the inverse of A to B to get badc.

Input: ('aaaa', 'bbbb', 'bbbb', 'cccc', 'cccc')

Expected Output: 'aaaa'

Explanation: Repeated characters are allowed as long as the mapping stays consistent.

Input: ('', '', '', '', '')

Expected Output: ''

Explanation: An empty message decodes to an empty message.

Input: ('ab', 'cd', 'cd', 'ef', 'eg')

Expected Output: ''

Explanation: The final message contains g, but the B to C sample does not provide a way to decrypt it.

Input: ('aa', 'ab', 'bc', 'de', 'de')

Expected Output: ''

Explanation: The A to B sample is inconsistent because a maps to both a and b.

Hints

  1. Build the inverse substitution map for each known cipher pair, because decoding goes from cipher character back to clear character.
  2. Validate consistency while building the map: one plaintext character cannot map to two ciphertext characters, and one ciphertext character cannot map to two plaintext characters.

Part 3: Compute Buffet Revenue with Capacity Limits and Repeated Visits

A buffet restaurant has a maximum seating capacity. You are given an array payments where payments[i] is how much customer i is willing to pay, and an event list events where each value is a customer ID. For each customer, the first appearance means they arrive, the second means they leave, the third means they arrive again, and so on. If a customer arrives when the restaurant is full, they wait outside until their matching leave event; they never take a seat during that visit. A customer pays only the first time they successfully get a seat, even if they visit again later. Return the total revenue earned.

Constraints

  • 0 <= capacity <= 100000
  • 0 <= len(payments) <= 100000
  • 0 <= len(events) <= 200000
  • 0 <= payments[i] <= 10^9
  • Every customer ID in events is a valid index into payments
  • For each customer, appearances alternate arrival and departure, starting with arrival

Examples

Input: (2, [10, 20, 30], [0, 1, 2, 0, 2])

Expected Output: 30

Explanation: Customers 0 and 1 get seats and pay. Customer 2 arrives when full, waits outside, and leaves without paying.

Input: (1, [5, 7], [0, 0, 0, 0, 1, 1])

Expected Output: 12

Explanation: Customer 0 visits twice but pays only once. Customer 1 pays on their first successful entry.

Input: (1, [4, 6], [0, 1, 0, 1, 1, 1])

Expected Output: 10

Explanation: Customer 1 first waits outside and pays nothing, then later returns when a seat is available and pays 6.

Input: (0, [8, 9], [0, 0, 1, 1])

Expected Output: 0

Explanation: With zero capacity, nobody can ever enter.

Input: (3, [2, 4], [])

Expected Output: 0

Explanation: No events means no customers and no revenue.

Hints

  1. Track each customer in one of three states: absent, inside, or waiting.
  2. Use a set to remember which customers have already paid so you never charge them twice.

Part 4: Decode an Anagram Sentence Using Vocabulary Constraints

You are given a vocabulary list and a sentence whose words have been scrambled. Each scrambled word is an anagram of exactly one vocabulary word, and it must have the same first and last character as the original word. Decode the sentence by replacing every scrambled word with its matching vocabulary word. If any scrambled word cannot be matched uniquely, return an empty string.

Constraints

  • 0 <= len(vocabulary) <= 100000
  • 0 <= total number of characters across all vocabulary words and the sentence <= 200000
  • All words contain lowercase English letters only
  • Vocabulary words are non-empty

Examples

Input: (['pears', 'spear', 'something'], 'seapr sothinmeg')

Expected Output: 'spear something'

Explanation: seapr matches spear because they are anagrams and both start with s and end with r.

Input: (['a'], '')

Expected Output: ''

Explanation: An empty sentence decodes to an empty sentence.

Input: (['a', 'b'], 'a b')

Expected Output: 'a b'

Explanation: Single-letter words are their own anagrams and have the same first and last character.

Input: (['listen', 'silent'], 'tinsel')

Expected Output: ''

Explanation: Although tinsel is an anagram, it does not share the first and last characters with either vocabulary word.

Input: (['reaps', 'rapes', 'pear'], 'rpeas')

Expected Output: ''

Explanation: rpeas could match both reaps and rapes under the given key, so decoding is ambiguous.

Hints

  1. A useful key is based on length, first character, last character, and the multiset of letters.
  2. Be careful: two different vocabulary words can have the same sorted letters, so you must include the first and last characters too.
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

  • Find Maximum Eastbound City Visits and Parse CSV - Upstart (medium)
  • Implement Byte Formatting and Cafeteria Billing - Upstart (medium)
  • Implement Three Assessment Functions - Upstart (medium)
  • Solve Five OA Coding Tasks - Upstart (medium)
  • Solve Reported OA Coding Problems - Upstart (medium)