PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates the ability to reconstruct and compose bijective character-substitution mappings, testing competency in string manipulation, mapping inference, and basic cipher reasoning.

  • medium
  • Upstart
  • Coding & Algorithms
  • Software Engineer

Decrypt a twice-encrypted message using known pairs

Company: Upstart

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Three people pass notes A → B → C → A. Notes are encrypted using a *character-substitution cipher* (a one-to-one mapping per character). You are given two known plaintext/ciphertext pairs that allow you to reconstruct the substitution keys: - From A → B: `clearMsgAb` and `cipheredMsgAb` - From B → C: `clearMsgBc` and `cipheredMsgBc` A third message (intended to be decrypted by A) has been encrypted twice in this order: 1) encrypt with A→B key 2) then encrypt with B→C key Resulting in: `cipheredMsgCba` To decrypt `cipheredMsgCba`, you must: 1) decrypt using the B→C key to obtain an intermediate string 2) decrypt the intermediate string using the A→B key to obtain the final plaintext Implement a function that returns the final decrypted plaintext. Inputs: - `string clearMsgAb` - `string cipheredMsgAb` - `string clearMsgBc` - `string cipheredMsgBc` - `string cipheredMsgCba` Assumptions (for a well-formed input): - `clearMsgAb.length == cipheredMsgAb.length` and `clearMsgBc.length == cipheredMsgBc.length`. - The substitution is consistent within each key (if `x` maps to `y` once, it always maps to `y`). - Only characters appearing in the known pairs will appear in `cipheredMsgCba` (so decryption is always possible).

Quick Answer: This question evaluates the ability to reconstruct and compose bijective character-substitution mappings, testing competency in string manipulation, mapping inference, and basic cipher reasoning.

Part 1: Reverse a List While Filtering Odd Numbers

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

Constraints

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

Examples

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

Expected Output: [4, 2]

Explanation: Reverse order is [4, 3, 2], then remove odd numbers.

Input: ([],)

Expected Output: []

Explanation: Edge case: empty input produces an empty list.

Input: ([1, 3, 5],)

Expected Output: []

Explanation: All numbers are odd, so nothing remains.

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

Expected Output: [8, 0, -2]

Explanation: Reverse is [8, 7, 0, -2, -3]; keep only evens.

Input: ([6],)

Expected Output: [6]

Explanation: Edge case: single even element.

Hints

  1. You do not need to physically reverse the whole list first; iterating from the end is enough.
  2. A number is even if num % 2 == 0.

Part 2: Decrypt a Twice-Encrypted Message Using Known Pairs

You are given two known plaintext/ciphertext pairs. Each pair was produced using a Caesar cipher with one fixed shift over lowercase English letters. The first pair reveals the A->B shift, and the second pair reveals the B->C shift. A message from C to A was encrypted twice: first with the A->B shift, then with the B->C shift. Decrypt the final ciphertext by reversing those two shifts.

Constraints

  • 1 <= len(clearMsgAb) == len(cipheredMsgAb) <= 10^5
  • 1 <= len(clearMsgBc) == len(cipheredMsgBc) <= 10^5
  • 0 <= len(cipheredMsgCba) <= 10^5
  • Each known pair uses one consistent Caesar shift for every character
  • All strings contain only lowercase letters a-z

Examples

Input: ('abc', 'bcd', 'xyz', 'zab', 'zruog')

Expected Output: 'world'

Explanation: A->B shift is 1, B->C shift is 2. Decrypt by -2, then -1.

Input: ('abc', 'abc', 'bcd', 'cde', '')

Expected Output: ''

Explanation: Edge case: empty final ciphertext decrypts to an empty string.

Input: ('az', 'cb', 'lmn', 'opq', 'cde')

Expected Output: 'xyz'

Explanation: Shifts are 2 and 3, so the total encryption shift was 5.

Input: ('hello', 'hello', 'world', 'world', 'same')

Expected Output: 'same'

Explanation: Both shifts are 0, so the message is unchanged.

Hints

  1. Recover each shift by comparing any plaintext character with its ciphertext counterpart.
  2. To undo double encryption, decrypt in the reverse order of encryption.

Part 3: Calculate Buffet Revenue with Capacity Limits and Repeat Visits

A buffet has limited seating capacity. payments[i] is how much customer i is willing to pay. events is a sequence of customer IDs. For each customer, the 1st, 3rd, 5th, ... appearance means that customer arrives; the 2nd, 4th, 6th, ... appearance means that customer leaves. If a customer arrives when the buffet is full, they join a FIFO waiting queue. Whenever a seat opens, the earliest waiting customer is seated immediately before the next event. A customer pays only once in total: the first time they actually get seated, even if they visit multiple times later. If a waiting customer leaves before getting seated, they pay nothing for that visit. Return the total revenue.

Constraints

  • 0 <= capacity <= 10^5
  • 0 <= len(payments) <= 10^5
  • 0 <= len(events) <= 2 * 10^5
  • 0 <= events[i] < len(payments) when events is non-empty
  • The event stream is valid: each customer's occurrences alternate arrival, departure, arrival, departure, ...

Examples

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

Expected Output: 60

Explanation: All three customers eventually get seated once, so revenue is 10 + 20 + 30.

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

Expected Output: 11

Explanation: Customer 1 waits, then gets seated immediately when customer 0 leaves.

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

Expected Output: 12

Explanation: Customer 1 is seated once from the waiting line and visits again later, but pays only on the first successful seating.

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

Expected Output: 0

Explanation: Edge case: capacity is zero, so nobody can ever be seated.

Input: (5, [], [])

Expected Output: 0

Explanation: Edge case: no customers and no events.

Hints

  1. Track which customers are currently seated and which are waiting.
  2. You need FIFO order for the waiting line, but a waiting customer may also leave before being seated.

Part 4: Decode an Anagram-Based Scrambled String Using a Vocabulary List

You are given a vocabulary list and a scrambled sentence. Every scrambled word is an anagram of exactly one vocabulary word, and it also has the same first and last character as the correct vocabulary word. Decode the full sentence. The uniqueness guarantee matters because some vocabulary words may share the same letters, such as 'pears' and 'spear'.

Constraints

  • 1 <= len(vocabulary) <= 10^5
  • The total length of all vocabulary words is at most 2 * 10^5
  • The total length of the scrambled sentence is at most 2 * 10^5
  • All words contain only lowercase English letters
  • The scrambled sentence may be empty
  • Each scrambled word matches exactly one vocabulary word

Examples

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

Expected Output: 'spear something'

Explanation: Both 'pears' and 'spear' have the same letters, but 'seapr' starts with 's' and ends with 'r', so it maps to 'spear'.

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

Expected Output: 'a'

Explanation: Edge case: single-letter word.

Input: (['listen', 'silent', 'evil', 'vile'], 'litsen vlie')

Expected Output: 'listen vile'

Explanation: The anagram key plus matching first/last letters gives a unique answer for each word.

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

Expected Output: ''

Explanation: Edge case: empty sentence.

Input: (['dusty', 'study'], 'sutdy dtsuy')

Expected Output: 'study dusty'

Explanation: These two vocabulary words are anagrams, so the first and last characters are needed to distinguish them.

Hints

  1. A plain character-count signature is not enough if two vocabulary words are anagrams of each other.
  2. Include the first and last character in the key you build for each word.
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)