# Errata for Data Structures & Algorithms in Kotlin 2nd Edition

**URL:** <https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193>\
**Category:** Data Structures and Algorithms in Kotlin\
**Created:** [June 29, 2021, 11:27pm UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193 "2021-06-29T23:27:21Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![jellodiil](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jellodiil/32/109742_2.png) [@jellodiil](https://forums.kodeco.com/u/jellodiil)\
**Post date:** [June 29, 2021, 11:27pm UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193/1 "2021-06-29T23:27:21Z")

</div>

Creating this topic to catch any typos and bugs in the 2nd Edition of Data Structures & Algorithms in Kotlin.

---

<div class="post-metadata">

**Author:** ![jellodiil](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jellodiil/32/109742_2.png) [@jellodiil](https://forums.kodeco.com/u/jellodiil)\
**Post date:** [June 29, 2021, 11:27pm UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193/2 "2021-06-29T23:27:38Z")

</div>



---

<div class="post-metadata">

**Author:** ![sjoerg](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/sjoerg/32/81494_2.png) [@sjoerg](https://forums.kodeco.com/u/sjoerg)\
**Post date:** [July 3, 2021, 10:58pm UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193/3 "2021-07-03T22:58:09Z")

</div>

There seem to be some problems with the challenge problem for Chapter 11, that is,  
implementing a function ArrayList.findIndices(value: T) where T: Comparable  
which returns a range of indices satisfying this[index] == value.

```
arrayListOf(0,1,3,3).findIndices(2)
// -> This will throw a StackOverflowException
// with the subroutine call 
// startIndex(2, 0..3) going into an infinite recursion 
// on the empty range 2..1

```

The problem here is with missing a check for emptiness of the range.

A second problem with both subfunctions startindex and endIndex on the pages 221 and 222 is the checking for the middleIndex lying on either boundary of the list.

```
val arr = arrayListOf(0,1)
arr.findIndices(0)
// → returns null where it should return 0..0

```

Here the first computed middleIndex 1 is already the right boundary of the list.  
Then the following will falsely return null in the previous example:

```
// 2 
if (middleIndex == 0 || middleIndex == size – 1) { // ?????
    return if (this[middleIndex] == value) {
        middleIndex 
    } else {
        null // ?????
    }
}

```

I didn’t quite succeed in fixing every issue with the book implementation though.  
There seem to be yet more issues with corner cases.  
I did however find a solution which is loosely based on the approach from the following video lecture

[![](https://img.youtube.com/vi/GU7DpgHINWQ/maxresdefault.jpg "Binary Search tutorial (C++ and Python)") ](https://www.youtube.com/watch?v=GU7DpgHINWQ)

Here’s an alternative solution:

```
/**
 * binaryFindFirst is only applicable on 'this' with argument prop if 
 *
 * this.map{ prop(this,it) } == [false, ... , false, true, ... , true]
 * or
 * this.map{ prop(this,it) } == [false, ... , false]
 * or 
 * this.map{ prop(this,it) } == [true , ... , true]
 * 
 * In other words:
 * If there exist indices i such that prop(this, i) == true 
 * then the list 'this' is partitioned such that all elements j with 
 * prop(this, j) == false lie on the left, and those indices 
 * i with prop(this,i) == true lie on the right.
 */
 
fun <T> List<T>.binaryFindFirst(
    prop: (l: List<T>, ind: Int) -> Boolean
): Int? {
    var res: Int? = null 
    var l = 0
    var r = this.lastIndex 
    while(l <= r) {
        val m = l + (r - l)/2 
        if (prop(this,m)) {
            res = m 
            r = m - 1
        }
        else 
            l = m + 1
    }
    return res
}

/**
 * binaryFindLast is only applicable on 'this' with argument prop if 
 *
 * this.map{ prop(this,it) } == [true, ... , true, false, ... , false]
 * or
 * this.map{ prop(this,it) } == [false, ... , false]
 * or 
 * this.map{ prop(this,it) } == [true , ... , true]
 *
 * In other words:
 * If there exist indices i such that prop(this, i) == true 
 * then the list 'this' is partitioned such that all elements j with 
 * prop(this, j) == false lie on the right, and those indices 
 * i with prop(this,i) == true lie on the left.
 */

fun <T> List<T>.binaryFindLast(
    prop: (l: List<T>, ind: Int) -> Boolean
): Int? {
    var res: Int? = null 
    var l = 0
    var r = this.lastIndex 
    while(l <= r) {
        val m = l + (r - l)/2 
        if (prop(this,m)) {
            res = m 
            l = m + 1
        }
        else 
            r = m - 1
    }
    return res    
}

/**
 * findIndices only applies to sorted lists.
 */
fun <T: Comparable<T>> List<T>.findIndices(value: T): IntRange? {
    // 1
    val left = binaryFindFirst { list, index ->
        list[index] >= value
    } ?: return null
    
    // 2
    if (this[left] != value) return null

    // 3
    val right = binaryFindLast { list, index -> 
        list[index] <= value
    } ?: return null

    // 4
    return left..right
}

/*
  1. The list must by assumption be sorted and any indices k 
     such that list[k] >= value are on the right.
     So binaryFindFirst is applicable.
 
  2. By assumption the list is sorted.
     Thus if the first index k such that list[k] >= value 
     does not satisfy list[k] == value, then list[k] > value
     and there can be no index j satisfying 
     list[j] == value. Hence we can return null.
 
  3. The list is by assumption sorted and all indices k such that 
     list[k] <= value are on the left (if existant).
     So binaryFindLast is applicable.
 
  4. If there exists any index k with list[k] == value,
     then all indices j within left..right satisfy list[j] == value.
 */

```

Note:

I guess technically for this problem we don’t need to pass both list and index to the lambdas. But this way the binaryFindFirst and binaryFindLast methods are also applicable in situations, where the property does not only depend on the list’s value at the current index.

For instance, there is a problem from the above video around minute 14:

Assume we are given a formerly sorted array from small to big, that was changed by applying a circular shift, e.g. [6,7,9,15,19,2,3] is the sorted array [2,3,6,7,9,15,19] after a two-fold circular left shift.

Find the smallest element in such a rotated sorted array.

In this case we can just use our binaryFindFirst method to find the first element smaller or equal to the leftmost entry.

```
fun <T: Comparable<T>> List<T>.findMinInRotatedList(): Int =
    binaryFindFirst { list, index -> list[index] <= list[0] }!!

```

Here the property does not only depend on the value at current index,  
but also on the value at 0.

[Edited for better names and readability.]

---

<div class="post-metadata">

**Author:** ![odaym](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/odaym/32/102911_2.png) [@odaym](https://forums.kodeco.com/u/odaym)\
**Post date:** [July 20, 2021, 7:44pm UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193/4 "2021-07-20T19:44:06Z")

</div>

Hi, great book, massive help so far.

There seems to be a double print of the code associated with Creating a Vertex, in Chapter 19 - Graphs. The code for `createVertex()` is printed twice in the code section, but each with a different set of steps.

![test2](https://files.chunter.kodeco.com/original/3X/6/2/6285bc7b3c6eb74531fbad23b4df795c87f4d338.jpeg)

The same thing happens in the same chapter, couple of pages further along, in the Visualize an adjacency matrix section, the toString() method is printed twice in the code section

Since I’m a new user here I cannot attach more than 1 media element but you can see this in the section mentioned above

Thanks a lot for all your efforts, this book helped a lot in everything

---

<div class="post-metadata">

**Author:** ![timothylyall](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timothylyall/32/98605_2.png) [@timothylyall](https://forums.kodeco.com/u/timothylyall)\
**Post date:** [August 13, 2021, 5:40pm UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193/6 "2021-08-13T17:40:16Z")

</div>

yes. thanks for information

---

<div class="post-metadata">

**Author:** ![cjp1tutor](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/cjp1tutor/32/81494_2.png) [@cjp1tutor](https://forums.kodeco.com/u/cjp1tutor)\
**Post date:** [August 14, 2021, 8:57pm UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193/7 "2021-08-14T20:57:27Z")

</div>

I already bought the 1st edition, how different is the 2nd edition??  
Is there an “errata” page that can save me another $60 ???

---

<div class="post-metadata">

**Author:** ![williamspete321](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/williamspete321/32/108131_2.png) [@williamspete321](https://forums.kodeco.com/u/williamspete321)\
**Post date:** [November 24, 2022, 6:13pm UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193/12 "2022-11-24T18:13:32Z")

</div>

@jellodiil There seems to be a mistake in Chapter 5 Queues, in DoubleLinkedList.kt’s remove() method:

The code is:

```auto
fun remove(node: Node<T>): T {

    val prev = node.previous
    val next = node.next

    if (prev != null) {
      prev.next = node.previous
    } else {
      head = next
    }

    next?.previous = prev

    if (next == null) {
      tail = prev
    }

    node.previous = null
    node.next = null

    return node.value
  }

```

However, I believe it’s meant to be the following code below. The change is inside the first if statement:

```auto
fun remove(node: Node<T>): T {

    val prev = node.previous
    val next = node.next

    if (prev != null) {
      prev.next = node.next
    } else {
      head = next
    }

    next?.previous = prev

    if (next == null) {
      tail = prev
    }

    node.previous = null
    node.next = null

    return node.value
  }

```

If a node is removed, then the previous node’s next value should point to the removed node’s next value: **prev.next = node.next** Does this seem accurate? Trying to use the current implementation produces errors if you remove elements from a Linked List and try to print out the new list.

---

<div class="post-metadata">

**Author:** ![jellodiil](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jellodiil/32/109742_2.png) [@jellodiil](https://forums.kodeco.com/u/jellodiil)\
**Post date:** [January 23, 2023, 10:44pm UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193/13 "2023-01-23T22:44:45Z")

</div>

Thanks for reporting this!  
I’ve noted it for the team to have a look!

---

<div class="post-metadata">

**Author:** ![sukakopi](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@sukakopi](https://forums.kodeco.com/u/sukakopi)\
**Post date:** [February 27, 2023, 8:44am UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193/14 "2023-02-27T08:44:03Z")

</div>

Hey guys! Please kindly visit my site : [https://www.aimeealways.com/](https://www.aimeealways.com/)

---

<div class="post-metadata">

**Author:** ![syrgdev](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@syrgdev](https://forums.kodeco.com/u/syrgdev)\
**Post date:** [April 14, 2023, 9:03pm UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193/15 "2023-04-14T21:03:04Z")

</div>

Hey guys. Seems to me that you have an error in the code on page 196 when removing TrieNode. Adding parent = parent.parent at the bottom of the while loop(right below the current = parent statment) will do the trick.

---

<div class="post-metadata">

**Author:** ![syrgdev](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@syrgdev](https://forums.kodeco.com/u/syrgdev)\
**Post date:** [April 16, 2023, 6:19pm UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193/16 "2023-04-16T18:19:46Z")

</div>

Page 227.  
Refactor suggestion:  
replace this block of code  
val leftChildIndex = index(element, leftChildIndex(i))  
if (leftChildIndex != null) return leftChildIndex // 4  
val rightChildIndex = index(element, rightChildIndex(i))  
if (rightChildIndex != null) return rightChildIndex // 5  
return null // 6

with this:  
return index(element, leftChildIndex(i))?: index(element, rightChildIndex(i))

Page 230.  
replace this:  
val inverseComparator = Comparator { o1, o2 → // 2  
override fun compare(o1: Int, o2: Int): Int =  
o2.compareTo(o1)  
}  
with this:  
val inverseComparator = Comparator { o1, o2 → // 2  
o2.compareTo(o1)  
}

---

<div class="post-metadata">

**Author:** ![primepainters](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/primepainters/32/110937_2.png) [@primepainters](https://forums.kodeco.com/u/primepainters)\
**Post date:** [March 30, 2026, 12:44pm UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193/27 "2026-03-30T12:44:24Z")

</div>

The cost of the project was worth every cent because the result is flawless **[interior painters Adelaide](https://primepainter.com.au/services/interior-painting-adelaide/)** and I know I won’t have to worry about painting the house again for years.

---

<div class="post-metadata">

**Author:** ![saautocaree](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/saautocaree/32/110937_2.png) [@saautocaree](https://forums.kodeco.com/u/saautocaree)\
**Post date:** [March 31, 2026, 1:40am UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193/28 "2026-03-31T01:40:42Z")

</div>

The exhaust system on my car was rattling quite **[Auto Mechanical Repair and Maintenance](https://saautocare.au/service-category/mechanical-services/)** a bit but a few new hangers and a small weld fixed the problem perfectly for a very low cost.

---

<div class="post-metadata">

**Author:** ![aurendering](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/aurendering/32/110937_2.png) [@aurendering](https://forums.kodeco.com/u/aurendering)\
**Post date:** [March 31, 2026, 6:15am UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193/29 "2026-03-31T06:15:02Z")

</div>

I appreciated the suggestions for the different types of beads **[High-Quality Texture Coating Materials](https://aurenderingplastering.com.au/services/texture-coating/)** to use on the corners to make sure they are extra durable and won’t get damaged by the kids.

---

<div class="post-metadata">

**Author:** ![jessehilton](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jessehilton/32/111871_2.png) [@jessehilton](https://forums.kodeco.com/u/jessehilton)\
**Post date:** [September 25, 2026, 5:22pm UTC](https://forums.kodeco.com/t/errata-for-data-structures-algorithms-in-kotlin-2nd-edition/143193/30 "2026-09-25T17:22:56Z")

</div>

Thanks for maintaining this errata thread. It’s really helpful to have corrections and updates in one place, especially when working through the examples in the book. I’ll keep this thread bookmarked in case any additional corrections are posted.
