Scala Cheatsheet
53 essential methods for coding interviews
Showing 53 of 53 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
map | list.map(f: A => B): List[B] | Transform each element using a function | O(n) | essential |
filter | list.filter(p: A => Boolean): List[A] | Keep only elements matching predicate | O(n) | essential |
flatMap | list.flatMap(f: A => IterableOnce[B]): List[B] | Map then flatten results into single collection | O(n * m) | essential |
foldLeft | list.foldLeft(z: B)(op: (B, A) => B): B | Reduce left-to-right with accumulator and initial value | O(n) | essential |
reduce | list.reduce(op: (A, A) => A): A | Reduce elements without initial value | O(n) | essential |
groupBy | list.groupBy(f: A => K): Map[K, List[A]] | Group elements by key function | O(n) | essential |
zip | list1.zip(list2): List[(A, B)] | Combine two lists into pairs | O(min(n, m)) | essential |
zipWithIndex | list.zipWithIndex: List[(A, Int)] | Pair each element with its index | O(n) | essential |
take / drop | list.take(n) / list.drop(n) | Take first n elements or drop first n elements | O(n) | essential |
head / tail | list.head / list.tail | First element / all except first | O(1) | essential |
sorted / sortBy / sortWith | list.sorted / list.sortBy(f) / list.sortWith(cmp) | Sort using natural order, key function, or comparator | O(n log n) | essential |
sliding | list.sliding(size, step): Iterator[List[A]] | Create sliding window iterator | O(n) | essential |
find | list.find(p: A => Boolean): Option[A] | Find first element matching predicate | O(n) | essential |
exists / forall | list.exists(p) / list.forall(p) | Check if any/all elements match predicate | O(n) | essential |
contains | list.contains(elem: A): Boolean | Check if element exists in collection | O(n) | essential |
Map apply / get | map(key) / map.get(key) | Access value by key (throws or returns Option) | O(1) average | essential |
getOrElse | map.getOrElse(key, default): V | Get value or return default if missing | O(1) average | essential |
updated / + | map.updated(key, value) / map + (key -> value) | Add or update key-value pair | O(1) average | essential |
keys / values | map.keys / map.values | Get iterable of keys or values | O(1) | essential |
Set + / - | set + elem / set - elem | Add or remove element from set | O(1) average | essential |
Set union / intersect / diff | set1 union set2 / set1 intersect set2 / set1 diff set2 | Set operations: union, intersection, difference | O(n + m) | essential |
Option map / flatMap | option.map(f) / option.flatMap(f) | Transform Option value if present | O(1) | essential |
Option getOrElse | option.getOrElse(default): A | Extract value or use default | O(1) | essential |
mkString | list.mkString(sep) / list.mkString(start, sep, end) | Join elements into string with separator | O(n) | essential |
split | string.split(regex): Array[String] | Split string by regex pattern | O(n) | essential |
String interpolation | s"text $var" / f"$num%d" / raw"\n" | Embed expressions in strings | O(n) | essential |
match expression | expr match { case pattern => result } | Pattern matching expression | O(1) to O(n) | essential |
case class unapply | case ClassName(a, b) => ... | Destructure case class in pattern match | O(1) | essential |
for comprehension | for { x <- list; y <- list2 } yield expr | Syntactic sugar for map/flatMap/filter | O(n * m) | essential |
Range / until / to | (start to end) / (start until end) / (start to end by step) | Create numeric ranges | O(1) creation | essential |
min / max / sum / product | list.min / list.max / list.sum / list.product | Aggregate operations on numeric collections | O(n) | essential |
minBy / maxBy | list.minBy(f) / list.maxBy(f) | Find min/max by key function | O(n) | essential |
collect | list.collect(pf: PartialFunction[A, B]): List[B] | Filter and map using partial function | O(n) | common |
partition | list.partition(p: A => Boolean): (List[A], List[A]) | Split into two lists based on predicate | O(n) | common |
last / init | list.last / list.init | Last element / all except last | O(n) | common |
distinct | list.distinct: List[A] | Remove duplicate elements preserving order | O(n) | common |
reverse | list.reverse: List[A] | Reverse the order of elements | O(n) | common |
flatten | list.flatten: List[B] | Flatten nested collections by one level | O(n) | common |
grouped | list.grouped(size): Iterator[List[A]] | Split into fixed-size chunks | O(n) | common |
indexOf | list.indexOf(elem: A): Int | Find index of first occurrence | O(n) | common |
count | list.count(p: A => Boolean): Int | Count elements matching predicate | O(n) | common |
Map removed / - | map.removed(key) / map - key | Remove key from map | O(1) average | common |
mapValues | map.view.mapValues(f).toMap | Transform all values in map | O(n) | common |
withDefaultValue | map.withDefaultValue(default): Map[K, V] | Create map with default value for missing keys | O(1) | common |
subsetOf | set1.subsetOf(set2): Boolean | Check if set1 is subset of set2 | O(n) | common |
Option orElse | option1.orElse(option2): Option[A] | Return first Option if Some, else second | O(1) | common |
Option fold | option.fold(ifEmpty)(f): B | Handle both Some and None cases | O(1) | common |
guard clause | case pattern if condition => result | Add condition to pattern match case | O(1) | common |
foreach | list.foreach(f: A => Unit): Unit | Execute side effect for each element | O(n) | common |
Option / Try / Either | Option[A] / Try[A] / Either[L, R] | Functional error handling types | O(1) | common |
span | list.span(p: A => Boolean): (List[A], List[A]) | Split at first element not matching predicate | O(n) | useful |
Option toList | option.toList: List[A] | Convert Option to List (0 or 1 element) | O(1) | useful |
LazyList / Stream | LazyList.from(n) / LazyList.iterate(start)(f) | Create lazy infinite sequences | O(1) per element | useful |