Return Every Matching Decryption
Updated
Problem statement
Build an encrypter from unique one-letter keys, their two-letter values, and a dictionary of distinct plaintext words. Different keys may have the same value.
Process every row [method, word] in operations:
encrypt: replace each letter ofwordwith its value. If any letter is not a key, the result is the empty string.decrypt: return the set of dictionary words whose encryption is exactlyword, instead of how many there are.
Return one row per operation. An encrypt row holds exactly its one encrypted string. A decrypt row holds the matching dictionary words. So that the answer can be checked, list them in the order they appear in dictionary. If no word matches, the row is empty.
Interview Follow-up
In one interview, the interviewer did not ask for the fastest solution. They asked for decrypt to be written with depth-first search and backtracking: read word two letters at a time, try every key whose value equals the next two letters, and undo that choice before trying the next key. They also asked why depth-first search fits this problem and how the backtracking works.
Function
processEncrypterMatches(keys: String[], values: String[], dictionary: String[], operations: String[][]) → String[][]Examples
Example 1
keys = ["a","b","c","d"]values = ["ei","zf","ei","am"]dictionary = ["abcd","acbd","adbc","badc","dacb","cadb","cbda","abad"]operations = [["encrypt","abcd"],["decrypt","eizfeiam"]]return = [["eizfeiam"],["abcd","abad"]]Encrypting abcd maps each letter to its value: ei, zf, ei, am. To decrypt eizfeiam, the blocks are ei, zf, ei, am. Block ei can come from a or c, so 2 * 1 * 2 * 1 = 4 plaintexts are possible: abad, abcd, cbad and cbcd. Only abcd and abad are in the dictionary, and they are listed in dictionary order.
Example 2
keys = ["a","b","c"]values = ["xy","xy","zz"]dictionary = ["ba","cab","ab","ca"]operations = [["decrypt","xyxy"],["decrypt","zzxy"],["encrypt","cad"]]return = [["ba","ab"],["ca"],[""]]Keys a and b share the value xy, so xyxy could be aa, ab, ba or bb. The dictionary has ba and ab. They are listed as ba, then ab, because that is their order in the dictionary, even though a search that tries a before b finds ab first. zzxy can only be ca or cb, and only ca is in the dictionary. d is not a key, so encrypting cad gives the empty string.
Example 3
keys = ["m"]values = ["pq"]dictionary = ["mn","m"]operations = [["decrypt","pqpq"],["decrypt","pq"]]return = [[],["m"]]pqpq could only be mm, which is not in the dictionary, so that row is empty. mn can never be decrypted, because n is not a key. pq decrypts to m.
Constraints
1 <= keys.length == values.length <= 26. Every key is a different lowercase English letter.- Every value is exactly two lowercase English letters. Values do not have to be different.
1 <= dictionary.length <= 100. Dictionary words are all different, and each has 1 to 100 lowercase English letters.1 <= operations.length <= 100. Each row is[method, word], wheremethodisencryptordecrypt.- An
encryptword has 1 to 2000 lowercase English letters. Adecryptword has an even number of lowercase English letters, from 2 to 200.
More Duolingo Phone Screen questions
- Group Timestamps into Consecutive-Day IntervalsPHONE SCREEN · Seen Nov 2026
- Visit Desired Attractions Without Reusing a TrailPHONE SCREEN · Seen Oct 2026
- Correct an In-Place Array ReversalPHONE SCREEN · Seen Oct 2026
- Count Starts Reaching a Grid MaximumPHONE SCREEN · Seen Oct 2026
- Select the Top User MatchesPHONE SCREEN · Seen Oct 2026
- Sum All Values in a Binary TreePHONE SCREEN · Seen Oct 2026
- User Resource Transition ProbabilitiesPHONE SCREEN · Seen Oct 2026
- Encrypt and Decrypt StringsPHONE SCREEN · Seen Apr 2026
- Validate a Mahjong Hand PartitionPHONE SCREEN · Seen Nov 2025
- Directional Tiles: Find a Valid Reconfiguration PathPHONE SCREEN · Seen Oct 2022
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- File Storage ServiceCresta · PHONE SCREENCresta interview questions
- Numeric Poker OperationsRippling · PHONE SCREENRippling interview questions
- Reference-Counted Deduplicating File SystemCitadel · PHONE SCREENCitadel interview questions
- Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
- String Copy and Move LifecycleSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Design In-Memory File SystemSnowflake · PHONE SCREENSnowflake interview questions
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
- Parcel Event TrackingAirbnb · OAAirbnb interview questions
- Account Scheduler - Availability and AcquireStripe · ONSITE INTERVIEWStripe interview questions