3 Interview-like Algorithm Questions for Programmers
post by lsusr · 2020-03-25T09:05:10.875Z · LW · GW · 18 commentsContents
1. Sorting 2. Search 3. Cryptography None 18 comments
These are my favorite interview-style algorithm questions for programmers. I like these questions because they test how well you understand information density instead of your ability to regurgitate facts.
Comments to help clarify puzzle details such as how the Enigma machine works are allowed. Otherwise, comments with hints, answers and spoilers will be deleted, even if they use special formatting to hide spoilers. PM me if you are unsure. Edit: I can't actually do this with less than 2000 karma. Please self-moderate responsibly.
1. Sorting
What is the fastest you can sort a list of int
s in Java?
Answer: time where is the length of the list
2. Search
You have 1000 saliva samples. Exactly 1 of them is from someone infected by COVID-19. You have unlimited testing strips. Each day the CDC has resources to process up to 10 testing strips for you. You must send all 10 testing strips to the CDC at the same time. The next day, the CDC will tell you which of the 10 strips has been exposed to infected saliva. What is the fewest number of days required to figure out which sample is infected?
Answer: 1 day
3. Cryptography
It is the year 1932. You work for the Polish government. They have captured a German Enigma machine. The Enigma machine has three rotating rubber rotors (disks). Each rotor has 26 copper wires feeding from one side to the other. At the back of the machine is a reflector, reflecting 13 of the contact points on the back of rotor 3 to the 13 other contact points on the back of rotor 3. In this way, the current gets scrambled forward through rotors 1, 2, and 3 and then scrambled backwards through rotors 3, 2 and 1. Each time someone presses a key, the first rotor rotates forward one letter. Every full rotation of the first rotor, the second rotor rotates one letter. Every full rotation of the second rotor, the third rotor rotates one letter.
Each letter of the alphabet is thus mapped to another letter of the alphabet in a symmetric cipher. This cipher changes each keystroke. You can configure the Enigma machine by setting the dials with a 3-letter code. The Germans change this code once per day. Today the daily code might be SEC. Tomorrow it could be UNK. The Germans also create a unique 3-letter code separately for each message. The daily code is used to transmit the message code twice.
For example, if the daily code is SEC and the message code is MES then the Germans will use the daily code SEC to encode MESMES. This might result in the ciphertext XFYZQM. Only these first 6 letters are encrypted with SEC. The rest of the message is encrypted with MES. The next message (on the same day) might have a message code COD. In this case, the daily code SEC is used to encode CODCOD which might result in the ciphertext IWVUBB.
Design the cheapest machine you can to break the German cryptosystem. You have access to neither vacuum tubes nor transistors. You may assume the Germans always use the same three rotors and never use the plugboard.
Hint: Marian Rejewski
18 comments
Comments sorted by top scores.
comment by Dan Weinand (dan-weinand) · 2020-03-25T23:59:32.709Z · LW(p) · GW(p)
These questions are way too 'Eureka!'/trivia for my taste. The first question relies on language specifics and then is really much more of a 'do you know the moderately weird sorting algs' question than an actual algorithms question. The second involves an oddly diluting resistant test. The third, again seems to test moderately well known crypto triva.
I've conducted ~60 interviews for Google and Waymo. To be clear, when I or most other interviewers I've seen say that a question covers 'algorithms' it means that it covers algorithm design. Ex. how do you find all pairs in an array whose sum is a given value? Such a question can be answered in several different ways, and the 'best' way involves using some moderately interesting data structures.
I'm super curious what kind of rubric you use in grading these questions.
Replies from: lsusr↑ comment by lsusr · 2020-03-26T02:15:40.487Z · LW(p) · GW(p)
If you happen to know the answer already then the question is ruined. In this way, every algorithm puzzle in the world can be ruined by trivia. For an algorithm question to be interesting, I hope the reader doesn't already know the answer and has to figure it out her/himself. So in questions #1 and #3, I'm hoping the reader doesn't know the relevant trivia and will instead independently derive the answers without looking them up.
I'm not trying to ask "Do you know how Poland cracked the Enigma?" I'm trying to ask "Can you figure out how Poland cracked the Enigma?"
I don't grade these questions. These questions are for fun and self-improvement. Though I could imagine a timed written test with dozens of questions like this where the testee gets one point for each correct answer and loses one point for each incorrect answer. A sufficiently large number of questions might help counteract the individual variance.
comment by Said Achmiz (SaidAchmiz) · 2020-03-25T20:53:57.841Z · LW(p) · GW(p)
Re: #1:
Which sorting algorithm shows O(n) time complexity given no assumptions but that the values are integers?
↑ comment by lsusr · 2020-03-25T22:02:35.077Z · LW(p) · GW(p)
There is no such algorithm.
↑ comment by Said Achmiz (SaidAchmiz) · 2020-03-26T02:52:17.271Z · LW(p) · GW(p)
… then your answer is wrong. So… what gives?
Replies from: lsusr↑ comment by lsusr · 2020-03-26T03:16:03.566Z · LW(p) · GW(p)
This question isn't about integers "given no assumptions". It's about the int
primitive in Java.
↑ comment by Matthew Barnett (matthew-barnett) · 2020-03-26T16:32:42.606Z · LW(p) · GW(p)
Nevermind.
↑ comment by Dagon · 2020-03-26T14:31:14.169Z · LW(p) · GW(p)
I think the trick you're going for (and I agree with others that these are mostly tricks - fun for conversation, interesting puzzles among friends, but fairly poor for interviews) requires a further restriction than an arbitrary array of Java primitive ints.
This is too close to an answer, so I'll further obscure it. I used rot-221 for extra secrecy!
V guvax gurl arrq gb or pbagvthbhf, fhpu gung gur enatr vf gur fnzr nf gur neenl fvmr. Gurer vf ab fbeg orggre guna B(AybtA) gung arrqf gb pbzcner ryrzragf gb rnpu bgure.
comment by Pattern · 2020-03-25T18:30:25.054Z · LW(p) · GW(p)
3. Crypto.
The Germans another create
create another
For example, if the daily code is SEC and the message code is MES then the Germans will use the daily code SEC to encode MESMES. This might result in the ciphertext XFYZQM. Only these first 6 letters are encrypted with SEC. The rest of the message is encrypted with MES. The next message (on the same day) might have a message code COD. In this case, the daily code SEC is used to encode CODCOD which might result in the ciphertext IWVUBB.
Let's imagine the daily code the machine is set it with is AAA.
Would this turn the plaintext "X" into the 'ciphertext' "X"?
But not "XX" to "XX", because typing changes the configuration?
Replies from: lsusr↑ comment by lsusr · 2020-03-25T20:04:04.074Z · LW(p) · GW(p)
Fixed typo.
Let's imagine the daily code the machine is set it with is AAA. Would this turn the plaintext "X" into the 'ciphertext' "X"?
No, for a couple reasons.
First of all, each of the rotors have a different wiring. For example, in the first rotor, A might be wired to B and B might be wired to M.
Secondly, the reflector makes it impossible for any letter to be mapped to itself.
You are correct that the plaintext AAA is unlikely to be encrypted into the same letter multiple times such as BBB, due to the rotating rotors.
Replies from: Patterncomment by philip_b (crabman) · 2020-04-25T12:48:04.236Z · LW(p) · GW(p)
I want to know why the answer to the first question is like that.
comment by Ericf · 2020-04-24T19:14:00.118Z · LW(p) · GW(p)
How many samples can be included on one test strip? This study of saliva based testing only looked at a 5x spread of copies/mL (200 - 1000) which implies that actual positive samples could be diluted beyond detection if too many negative samples are included.
Replies from: lsusr