cswork / libraryOpen practice workspace

Coding questions · Upstart

Decrypt a twice-encrypted message using known pairs

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

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).

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

Example 1

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

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

Example 2

Input: ([],)
Output: []

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

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

Example 1

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

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

Example 2

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

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

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

Example 1

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

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

Example 2

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

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

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

Example 1

Input: (['pears', 'spear', 'something'], 'seapr sothinmeg')
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'.

Example 2

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

Explanation: Edge case: single-letter word.

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.