Decrypt a twice-encrypted message using known pairs
Company: Upstart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
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
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
- You do not need to physically reverse the whole list first; iterating from the end is enough.
- A number is even if num % 2 == 0.
Part 2: Decrypt a Twice-Encrypted Message Using Known Pairs
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
- Recover each shift by comparing any plaintext character with its ciphertext counterpart.
- To undo double encryption, decrypt in the reverse order of encryption.
Part 3: Calculate Buffet Revenue with Capacity Limits and Repeat Visits
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
- Track which customers are currently seated and which are waiting.
- 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
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
- A plain character-count signature is not enough if two vocabulary words are anagrams of each other.
- Include the first and last character in the key you build for each word.