igorfyago / Coding-challenges

real life interviews, pramp mock interviews, HackerRank code challenges ...

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Coding-challenges

scala coding challenges

Return the k most frequent values from a list

object KMaxFrequent extends App {
  examples.example map function foreach println
}

object KMaxFrequentChallenge {
  val examples = Examples(List(
      Example(data = List(3, 5, 1, 4, 1, 2, 1, 4, 4, 1, 0, 5),  k = 3) // => 1, 4, 5
    , Example(data = List("A", "B", "C", "C", "D", "C", "B"),   k = 2) // => C, B
    , Example(data = List("A", 3, 3, 3, "D", "C", "B"),         k = 4) // => 3, A, B, C
  ))
}

object KMaxFrequentSolution {
  val function: Example => String = example =>
    example.data.par
      .groupBy(i => i)
      .to.sortBy(-_._2.size)
      .take(example.k)
      .map(_._1)
      .mkString(", ")
}

Return the max distinct count among sliding sublists of size k

object MaxSlidingDistinctCount extends App {
  examples.example map function foreach println
}

object MaxSlidingDistinctCountChallenge {
  val examples = Examples(List(
      Example(data = List(1, 1, 3, 3, 3, 3, 5, 3),      k = 4) // => 2, List(1, 1, 3, 3)
    , Example(data = List("A", "A", "A", "C", 5, "C"),  k = 3) // => 3, List(A, C, 5)
    , Example(data = List("A", "A", "A"),               k = 2) // => 1, List(A, A)
  ))
}

object MaxSlidingDistinctCountSolution {
  val function: Example => Int = example =>
    example.data.par
      .to.sliding(example.k)
      .map(_.distinct.size)
      .max
}

About

real life interviews, pramp mock interviews, HackerRank code challenges ...


Languages

Language:Java 58.6%Language:Scala 37.1%Language:Python 4.3%