Skip to content

Instantly share code, notes, and snippets.

@groz
Created October 10, 2015 00:27
Show Gist options
  • Save groz/d4d053819e4ff661688c to your computer and use it in GitHub Desktop.
Save groz/d4d053819e4ff661688c to your computer and use it in GitHub Desktop.
Facebook's how to crush your coding interview example in Scala.
object FBSampleApp extends App {
// sample implementations
def nearbyChars(c: Char): Set[Char] = Set(c + 0, c + 1).map(_.toChar)
def isWord(s: String): Boolean = true
def distSoFar(a: String, b: String) = a.zip(b).count(ab => ab._1 != ab._2)
def nearbyWords(input: String, maxDist: Int = 1): Set[String] =
input.foldLeft(Set("")) { (words, char) =>
for {
c <- nearbyChars(char)
w <- words
word = w + c
if distSoFar(input, word) <= maxDist
} yield word
}.filter(isWord)
println(nearbyWords("Hello"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment