private val items: MutableList<T?> = mutableListOf()can be written more succinctly asprivate val items = mutableListOf<T?>()(the type is inferred and then no longer repeated (mostly) in an explicit type definition and the factory method call).sizecan be backed byitems.size:val size: Int get() = items.size fun push(item: T) { set(size + 1, item) siftUp(size - 1) } ... fun pop(): T? { ... set(0, get(size - 1)) items.removeAt(items.lastIndex) // Remove original reference to the last item ... }siftDownthrows anAssertionErrorassertion whensize == 0.pushandpophave different meanings in the Java Collections Framework associated with using a java.util.Deque as a stack. You might instead use the same verbs as java.util.PriorityQueue (namelyaddandremove) which can be used as a max heap:fun <T : Comparable<T>> maxHeap() = PriorityQueue<T>(Comparator.reverseOrder<T>())