Skip to content

Instantly share code, notes, and snippets.

@gbajaj
gbajaj / BFSRecursiveTraversal.kt
Created July 6, 2025 07:04
Traverses and collects node values in a BFS Tree Traversal
class Solution {
fun levelOrder(root: TreeNode?): List<List<Int>> {
val twoDList = mutableListOf<MutableList<Int>>()
levelOrderRecursive(root, twoDList, 0)
return twoDList
}
fun levelOrderRecursive(root: TreeNode?, list: MutableList<MutableList<Int>>, level: Int) {
if (root == null) return
@gbajaj
gbajaj / IterativeTreeLevelOrderTraversal.kt
Created July 5, 2025 21:59
Traverses tree level by level iteratively
/**
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
@gbajaj
gbajaj / 2DMutableToImmutable.kt
Created July 5, 2025 21:14
Snippet that defines an empty mutable 2D list, adds values to it, and then converts it into a fully immutable 2D list.
fun main() {
// 1. Define an empty mutable 2D list
val twoDList = mutableListOf<MutableList<Int>>()
println("1. Initial empty mutable list: $twoDList")
// 2. Add inner lists to it later
twoDList.add(mutableListOf(10, 20, 30))
twoDList.add(mutableListOf(40, 50, 60))
println("2. Mutable list after adding rows: $twoDList")
@gbajaj
gbajaj / CustomRecordExample.kt
Created July 4, 2025 23:28
Convert int [][] into a list of custom Records
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class CustomRecordExample {
// Your static nested class is defined here
static class Record {
int first;
@gbajaj
gbajaj / ArrayListIntegerToIntArray.kt
Created July 4, 2025 23:13
Convert ArrayList<Integer> to a primitive int[] array
import java.util.ArrayList;
import java.util.Arrays;
// Your ArrayList of Integers
ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
// Convert it to a primitive int[] array
int[] primitiveArray = arrayList.stream()
.mapToInt(Integer::intValue)
.toArray();
@gbajaj
gbajaj / arrToArrayListOfIntegers.java
Created July 4, 2025 23:11
Converts int [] to ArrayList<Integer>
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
int[] primitiveArray = {1, 2, 3, 4, 5};
// Use Arrays.stream() to convert the int[] to an IntStream, then collect it
ArrayList<Integer> arrayList = Arrays.stream(primitiveArray)
.boxed() // Converts int to Integer
.collect(Collectors.toCollection(ArrayList::new));
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
/**
* A ViewModel to manage the logic of a single item in our shopping cart.
*/
class ShoppingCartItemViewModel {
// A private MutableStateFlow to hold the current quantity of the item.
// This will be updated by the UI when the user clicks the '+' or '-' buttons.
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
/**
* A ViewModel to manage the logic of a single item in our shopping cart.
*/
class ShoppingCartItemViewModel {
// A private MutableStateFlow to hold the current quantity of the item.
// This will be updated by the UI when the user clicks the '+' or '-' buttons.
@gbajaj
gbajaj / kt
Created July 2, 2025 05:12
flatMapLatest
fun <T, R> Flow<T>.flatMapLatest(transform: suspend (T) -> Flow<R>): Flow<R> = flow {
// This coroutineScope is key to managing cancellation of the inner flow
coroutineScope {
var previousJob: Job? = null
collect { value ->
// Cancel any ongoing inner flow collection
previousJob?.cancelAndJoin()
// Launch new inner flow collector in a new coroutine
@gbajaj
gbajaj / .kt
Created July 2, 2025 05:01
DistinctUntilChanged
fun <T> Flow<T>.distinctUntilChanged(comparator: (T, T) -> Boolean): Flow<T> = flow {
var previous: T? = null
var isFirst = true
collect { value ->
if (isFirst || !comparator(previous!!, value)) {
emit(value)
previous = value
isFirst = false
}