Kotlin Cheatsheet
45 essential methods for coding interviews
Showing 45 of 45 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
listOf / mutableListOf | listOf(elements) / mutableListOf(elements) | Creates immutable or mutable list from elements | O(n) | essential |
add / removeAt | list.add(element) / list.removeAt(index) | Adds element to end or removes element at index | O(1) add / O(n) removeAt | essential |
get / [] | list[index] / list.getOrNull(index) | Access element at index; getOrNull returns null if out of bounds | O(1) | essential |
size / isEmpty | list.size / list.isEmpty() / list.isNotEmpty() | Returns count of elements or checks if empty | O(1) | essential |
first / last | list.first() / list.last() / list.firstOrNull() | Returns first/last element; throws if empty without OrNull variant | O(1) | essential |
length / isEmpty | str.length / str.isEmpty() / str.isBlank() | Returns character count or checks if empty/blank | O(1) / O(n) for isBlank | essential |
substring | str.substring(startIndex, endIndex) | Extracts substring from start to end (exclusive) | O(n) | essential |
split / joinToString | str.split(delimiter) / list.joinToString(separator) | Splits string into list or joins list elements into string | O(n) | essential |
toCharArray | str.toCharArray() | Converts string to CharArray for mutation | O(n) | essential |
toInt / toIntOrNull | str.toInt() / str.toIntOrNull() | Parses string to integer; toIntOrNull returns null on failure | O(n) | essential |
contains / startsWith / endsWith | str.contains(s) / str.startsWith(prefix) | Checks for substring, prefix, or suffix presence | O(n*m) contains / O(k) prefix | essential |
mapOf / mutableMapOf | mapOf(k1 to v1) / mutableMapOf<K, V>() | Creates immutable or mutable HashMap | O(1) average operations | essential |
get / getOrDefault | map[key] / map.getOrDefault(key, default) | Returns value for key, or null/default if not present | O(1) average | essential |
getOrPut | map.getOrPut(key) { defaultValue } | Returns existing value or computes, stores, and returns default | O(1) average | essential |
containsKey / in | key in map / map.containsKey(key) | Checks if map contains the specified key | O(1) average | essential |
keys / values / entries | map.keys / map.values / map.entries | Returns set of keys, collection of values, or set of entries | O(1) | essential |
setOf / mutableSetOf | setOf(elements) / mutableSetOf(elements) | Creates immutable or mutable HashSet | O(n) | essential |
add / contains (Set) | set.add(element) / element in set | Adds element (returns true if new) or checks membership | O(1) average | essential |
map | collection.map { transform } | Transforms each element, returns new list | O(n) | essential |
filter / filterNot | collection.filter { predicate } | Returns list of elements matching (or not matching) predicate | O(n) | essential |
reduce / fold | list.reduce { acc, e -> } / list.fold(init) { acc, e -> } | Accumulates values; fold takes initial value, reduce uses first element | O(n) | essential |
groupBy | collection.groupBy { keySelector } | Groups elements by key into Map<K, List<V>> | O(n) | essential |
sortedBy / sortedByDescending | collection.sortedBy { selector } | Returns list sorted by comparable key selector | O(n log n) | essential |
sorted / sortedDescending | collection.sorted() / collection.sortedDescending() | Returns new list sorted in natural or reverse order | O(n log n) | essential |
sortedWith / compareBy | list.sortedWith(compareBy { it.x }) | Sorts with custom comparator; compareBy for multi-level sorting | O(n log n) | essential |
binarySearch | list.binarySearch(element) | Searches sorted list; returns index or negative insertion point | O(log n) | essential |
let | obj?.let { it -> ... } | Executes block with object as "it", returns block result | O(1) | essential |
apply | obj.apply { this -> ... } | Executes block with "this" context, returns the object itself | O(1) | essential |
maxOrNull / minOrNull | collection.maxOrNull() / collection.maxByOrNull { } | Returns max/min element or null if empty | O(n) | essential |
sum | collection.sum() | Returns sum of all numeric elements | O(n) | essential |
indices / withIndex | list.indices / list.withIndex() | Returns index range or iterable of (index, value) pairs | O(1) | common |
reversed | list.reversed() | Returns new list with elements in reversed order | O(n) | common |
slice / subList | list.slice(range) / list.subList(from, to) | Returns list portion; subList is a view, slice creates new list | O(k) slice / O(1) subList | common |
trim / replace | str.trim() / str.replace(old, new) | Removes whitespace or replaces all occurrences | O(n) | common |
remove (Map) | map.remove(key) | Removes entry for key, returns previous value or null | O(1) average | common |
toSet | collection.toSet() | Converts collection to Set, removing duplicates | O(n) | common |
flatMap | collection.flatMap { transform } | Maps each element to iterable and flattens results | O(n * m) | common |
any / all / none | collection.any { } / .all { } / .none { } | Checks if any/all/none elements match predicate | O(n) | common |
count / sumOf | list.count { predicate } / list.sumOf { selector } | Counts matching elements or sums selected values | O(n) | common |
take / drop | list.take(n) / list.drop(n) | Returns first n elements / returns list without first n | O(n) | common |
distinct | collection.distinct() / collection.distinctBy { } | Returns list with only distinct elements | O(n) | common |
also | obj.also { it -> ... } | Executes block with object as "it", returns the object | O(1) | common |
run / with | obj.run { } / with(obj) { } | Executes block with "this" context, returns block result | O(1) | common |
zip | list1.zip(list2) | Returns list of pairs combining elements at same indices | O(min(n, m)) | common |
windowed | collection.windowed(size, step) | Returns sliding windows of specified size | O(n) | useful |