Ransom Note
Published on 2020-5-3 by Tristan Sweeney
Intro
Given the text for a ransom note, determine if enough letters exist in a magazine to create it.
Dark premise, but achievable. I’d call this problem easy, but hard to do optimally if you don’t have the right tool (e.g. frequency-map) in the mental toolbox. I’ve annotated the code with time/space complexity thoughts, and their leetcode submission runtimes.
I guided a friend through discovering to the optimal solution, and found it was easiest to reach that solution when he wasn’t thinking of code. Pushing him to describe “what process would you follow” got him envisioning what that optimal solution would look like from a process perspective, short-circuiting the impulse to write some code and stare at it wondering “how could this be quicker”. Once I got an answer, prodding him with “would that be optimal?” was enough to get a response of “oh, of course not, I’d tally up what letters I needed then work from that”. A final “would you keep looking at the magazine after you found all the letters you needed?” finished the journey, leaving him with an implementation similar to what I have below.
It was a good practice for teaching and interviewing, getting someone out of a problem-solving rut without handing them a solution takes care, and it’s been better to clutz with a friend rather then a student or applicant.
Written by Tristan Sweeney
← Back to blog