A problem that has shown up multiple times in my preparation for technical interviews is the so called Ransom Note problem.
To solve this in F# is pretty simple.
In the interest of efficiency I decided to use a mutable Dictionary instead of the F# Map to store the no. of times a letter appears in the ransom note.
There’s also an optimization here to stop iterating through the magazine once all the letters for the ransom note is accounted for. One way to do it is to use Seq.takeWhile and mutate the Dictionary as we iterate through letters in the magazine, and stop when letters.Count = 0.
Try it Yourself
- A Java implementation
- Interview question on CareerCup
- DotNetFiddle snippet
- All my Project Euler solutions in F#
- All my Advent of Code solutions in F#