# Data Structures & Algorithms in Swift

**URL:** https://forums.kodeco.com/c/books/data-structures-algorithms-swift/40.md?page=2

[Latest](https://forums.kodeco.com/latest.md) · [Categories](https://forums.kodeco.com/categories.md) · [Tags](https://forums.kodeco.com/tags.md)

**Page:** 3

---

## [How should I know which DS will be applicable to story based given problem](https://forums.kodeco.com/t/how-should-i-know-which-ds-will-be-applicable-to-story-based-given-problem/138145)

<div class="topic-metadata">

**Author:** [@wredman](https://forums.kodeco.com/u/wredman)\
**Replies:** 0\
**Last updated:** [April 20, 2021, 5:36am UTC](https://forums.kodeco.com/t/how-should-i-know-which-ds-will-be-applicable-to-story-based-given-problem/138145 "2021-04-20T05:36:49Z")

</div>

like Competitive programming questions are likely to be story baed. So how can i understand which algo and DS will be applicable for it. like arrays, linkedlist, stacks, queues like wise please help.

---

## [Why is the Node implemented as a class and not a struct?](https://forums.kodeco.com/t/why-is-the-node-implemented-as-a-class-and-not-a-struct/135214)

<div class="topic-metadata">

**Author:** [@wao218](https://forums.kodeco.com/u/wao218)\
**Replies:** 1\
**Last updated:** [March 27, 2021, 3:06pm UTC](https://forums.kodeco.com/t/why-is-the-node-implemented-as-a-class-and-not-a-struct/135214 "2021-03-27T15:06:44Z")

</div>

I was wondering if anyone could explain why the Node object is implemented as a class instead of a struct. Wouldn’t implementing the Node class as a struct allow it to conform to value semantics? Wouldn’t a struct elimin…

---

## [Chapter 37: Graph Challenges](https://forums.kodeco.com/t/chapter-37-graph-challenges/113683)

<div class="topic-metadata">

**Author:** [@pulkitap](https://forums.kodeco.com/u/pulkitap)\
**Replies:** 1\
**Last updated:** [March 19, 2021, 12:39pm UTC](https://forums.kodeco.com/t/chapter-37-graph-challenges/113683 "2021-03-19T12:39:14Z")

</div>

In challenge 1 solution, we are passing references to “paths” function and then finding out the number of paths from one vertex to another, which seems to be quite confusing in first look. But as per my solution(plz par…

---

## [Chapter 25: Priority Queue](https://forums.kodeco.com/t/chapter-25-priority-queue/112609)

<div class="topic-metadata">

**Author:** [@pulkitap](https://forums.kodeco.com/u/pulkitap)\
**Replies:** 3\
**Last updated:** [January 3, 2021, 12:56pm UTC](https://forums.kodeco.com/t/chapter-25-priority-queue/112609 "2021-01-03T12:56:18Z")

</div>

Hi, Actually in this chapter, it is mentioned that Swift default “sort” function uses “Introsort” in the background. But with the release of Swift 5, this “sort” function started using “Timsort”. And as this book is u…

---

## [Chapter 19 challenge question wording](https://forums.kodeco.com/t/chapter-19-challenge-question-wording/128076)

<div class="topic-metadata">

**Author:** [@hlung](https://forums.kodeco.com/u/hlung)\
**Replies:** 1\
**Last updated:** [December 21, 2020, 6:14pm UTC](https://forums.kodeco.com/t/chapter-19-challenge-question-wording/128076 "2020-12-21T18:14:36Z")

</div>

Trie Challenge 1 where it says The second implementation uses a trie of strings. If the symbol database contains a total of 1,000,000 entries, and four entries contain symbols with prefix “pri” consisting of “prior”, “…

---

## [Chapter 12.2 Binary tree traversal algorithms](https://forums.kodeco.com/t/chapter-12-2-binary-tree-traversal-algorithms/128089)

<div class="topic-metadata">

**Author:** [@hlung](https://forums.kodeco.com/u/hlung)\
**Replies:** 1\
**Last updated:** [December 21, 2020, 6:09pm UTC](https://forums.kodeco.com/t/chapter-12-2-binary-tree-traversal-algorithms/128089 "2020-12-21T18:09:43Z")

</div>

It hard for me how to memorize the difference between each traversal algorithms… until I see the code side-by-side in the playground: public func traversePreOrder(visit: (Element) -\> Void) { visit(value) leftChild?.…

---

## [LinkedList conformance for Collection](https://forums.kodeco.com/t/linkedlist-conformance-for-collection/126911)

<div class="topic-metadata">

**Author:** [@hector818](https://forums.kodeco.com/u/hector818)\
**Replies:** 0\
**Last updated:** [December 8, 2020, 6:19am UTC](https://forums.kodeco.com/t/linkedlist-conformance-for-collection/126911 "2020-12-08T06:19:00Z")

</div>

extension LinkedList: Collection { public struct Index: Comparable { public var node: Node\<Value\>? static public func ==(lhs: Index, rhs: Index) -\> Bool { switch (lhs.node, rhs.no…

---

## [Chapter 3: Copy on write question](https://forums.kodeco.com/t/chapter-3-copy-on-write-question/125324)

<div class="topic-metadata">

**Author:** [@krissense](https://forums.kodeco.com/u/krissense)\
**Replies:** 0\
**Last updated:** [November 19, 2020, 4:08pm UTC](https://forums.kodeco.com/t/chapter-3-copy-on-write-question/125324 "2020-11-19T16:08:05Z")

</div>

Hi! I was wondering how it is supposed to work when linked list doesn’t have a references to it (which means that isKnownUniquelyReferenced is true) and we want to remove node after given node? As far as I can see meth…

---

## [Space Complexity of passed Array](https://forums.kodeco.com/t/space-complexity-of-passed-array/125242)

<div class="topic-metadata">

**Author:** [@johnborchert](https://forums.kodeco.com/u/johnborchert)\
**Replies:** 0\
**Last updated:** [November 18, 2020, 8:20pm UTC](https://forums.kodeco.com/t/space-complexity-of-passed-array/125242 "2020-11-18T20:20:15Z")

</div>

For chapter 2 Complexity. func printSorted(\_ array: \[Int\]) { let sorted = array.sorted() for element in sorted { print(element) } } You say that the space complexity is O(N) because of the new sorted array. In Sw…

---

## [Linked List Challenge 3](https://forums.kodeco.com/t/linked-list-challenge-3/124813)

<div class="topic-metadata">

**Author:** [@humayunharoon](https://forums.kodeco.com/u/humayunharoon)\
**Replies:** 0\
**Last updated:** [November 12, 2020, 3:05pm UTC](https://forums.kodeco.com/t/linked-list-challenge-3/124813 "2020-11-12T15:05:46Z")

</div>

Hey, I was solving Linked Lists Challenge 3 Linked List Challenges and I pretty much managed to write the exact solution minus one line. Without that one line the solution gets stuck, but I can’t figure out why. The acc…

---

## [Errata Chapter 8 Data Structures & Algorithms in Swift](https://forums.kodeco.com/t/errata-chapter-8-data-structures-algorithms-in-swift/122417)

<div class="topic-metadata">

**Author:** [@wolfandrei](https://forums.kodeco.com/u/wolfandrei)\
**Replies:** 0\
**Last updated:** [October 12, 2020, 10:33am UTC](https://forums.kodeco.com/t/errata-chapter-8-data-structures-algorithms-in-swift/122417 "2020-10-12T10:33:31Z")

</div>

There is a little mistake in implementation a queue using ring buffer, namely in ring buffer class: var first: T? { array\[readIndex\] } it should be: var first: T? { return isEmpty ? nil : array\[readIndex % array.co…

---

## [Feedback / Errata](https://forums.kodeco.com/t/feedback-errata/42125)

<div class="topic-metadata">

**Author:** [@seano](https://forums.kodeco.com/u/seano)\
**Replies:** 13\
**Last updated:** [September 11, 2020, 6:42am UTC](https://forums.kodeco.com/t/feedback-errata/42125 "2020-09-11T06:42:31Z")

</div>

In v0.9.1, p.99, the “post-swap” diagram has minor mistake: remove dotted line & add solid line between node 27 and 37. I guess the text on p.98 also is not clear (or is inferred) that once node 27 is swapped into place…

---

## [Tries clarification](https://forums.kodeco.com/t/tries-clarification/111237)

<div class="topic-metadata">

**Author:** [@ghostfaceace](https://forums.kodeco.com/u/ghostfaceace)\
**Replies:** 2\
**Last updated:** [July 30, 2020, 4:05pm UTC](https://forums.kodeco.com/t/tries-clarification/111237 "2020-07-30T16:05:42Z")

</div>

Hello, I wanted some clarification on Tries, so if I have an array: var array = \["dog", "cat", "bird"\] And I insert these elements into the trie, the trie converts these strings to arrays of the characters of the stri…

---

## [Cahpter 07: Challenge 4: Merge two lists?](https://forums.kodeco.com/t/cahpter-07-challenge-4-merge-two-lists/112721)

<div class="topic-metadata">

**Author:** [@hyegon](https://forums.kodeco.com/u/hyegon)\
**Replies:** 1\
**Last updated:** [July 1, 2020, 11:07am UTC](https://forums.kodeco.com/t/cahpter-07-challenge-4-merge-two-lists/112721 "2020-07-01T11:07:11Z")

</div>

I copied mergeSorted() from the final code. Only input values ​​were as follows. code… example(of: “Merge two lists”) { var list1 = LinkedList() list1.push(1) list1.push(4) list1.push(10) list1.push(11) var list2…

---

## [Question about struct vs class](https://forums.kodeco.com/t/question-about-struct-vs-class/61980)

<div class="topic-metadata">

**Author:** [@prutha](https://forums.kodeco.com/u/prutha)\
**Replies:** 4\
**Last updated:** [June 24, 2020, 8:38pm UTC](https://forums.kodeco.com/t/question-about-struct-vs-class/61980 "2020-06-24T20:38:02Z")

</div>

Hello All, I have question regarding the choice of struct vs class in some of the data structure. Why the book has some data structures defined using struct like heap but some are defined as class like LinkedList. What…

---

## [Chapter 8 - Can't find DoublyLinkedList class in the Sources folder](https://forums.kodeco.com/t/chapter-8-cant-find-doublylinkedlist-class-in-the-sources-folder/94060)

<div class="topic-metadata">

**Author:** [@mkashiwagi](https://forums.kodeco.com/u/mkashiwagi)\
**Replies:** 7\
**Last updated:** [June 3, 2020, 3:55pm UTC](https://forums.kodeco.com/t/chapter-8-cant-find-doublylinkedlist-class-in-the-sources-folder/94060 "2020-06-03T15:55:35Z")

</div>

Hi in the example of the Doubly linked list implementation it mentions there is a DoublyLinkedList class in the Sources folder but I cannot see it, only the Array one.

---

## [Swift algorithm club vs book](https://forums.kodeco.com/t/swift-algorithm-club-vs-book/41812)

<div class="topic-metadata">

**Author:** [@appledeveloper](https://forums.kodeco.com/u/appledeveloper)\
**Replies:** 4\
**Last updated:** [May 4, 2020, 4:23am UTC](https://forums.kodeco.com/t/swift-algorithm-club-vs-book/41812 "2020-05-04T04:23:22Z")

</div>

Whats the difference between the Data Structures & Algorithms in Swift book and the free available swift-algorithm-club? Thanks in advance

---

## [Is there any major difference b/w 1st edition & 3rd edition of Data-structures & Algorithms in Swift Book by Ray Wanderlich](https://forums.kodeco.com/t/is-there-any-major-difference-b-w-1st-edition-3rd-edition-of-data-structures-algorithms-in-swift-book-by-ray-wanderlich/87911)

<div class="topic-metadata">

**Author:** [@chethu](https://forums.kodeco.com/u/chethu)\
**Replies:** 3\
**Last updated:** [February 27, 2020, 8:00am UTC](https://forums.kodeco.com/t/is-there-any-major-difference-b-w-1st-edition-3rd-edition-of-data-structures-algorithms-in-swift-book-by-ray-wanderlich/87911 "2020-02-27T08:00:36Z")

</div>

Hello, Venerated Swift Developers, I am having Interviews in the next few weeks for ios developer’s job. I had purchased the 1st edition of Raywenderlich Data Structure & Algorithms in Swift language previously to prep…

---

## [AVL tree balance factors](https://forums.kodeco.com/t/avl-tree-balance-factors/97973)

<div class="topic-metadata">

**Author:** [@blwinters](https://forums.kodeco.com/u/blwinters)\
**Replies:** 1\
**Last updated:** [February 18, 2020, 4:04pm UTC](https://forums.kodeco.com/t/avl-tree-balance-factors/97973 "2020-02-18T16:04:39Z")

</div>

In this book (p. 183, 3rd ed.) the balance factor is computed as leftHeight - rightHeight. But in the Wikipedia article the calculation is reversed, rightHeight - leftHeight. I’m wondering if this book’s approach is comm…

---

## [Doubly-linked, circular, bi-directional linkedList](https://forums.kodeco.com/t/doubly-linked-circular-bi-directional-linkedlist/90363)

<div class="topic-metadata">

**Author:** [@papavector](https://forums.kodeco.com/u/papavector)\
**Replies:** 1\
**Last updated:** [January 13, 2020, 4:41am UTC](https://forums.kodeco.com/t/doubly-linked-circular-bi-directional-linkedlist/90363 "2020-01-13T04:41:15Z")

</div>

I am working on a Swift implementation of dancing links algorithm to solve exactCover problems. I need a linked list (linked matrix actually) that can be navigated in 2-D (left/right, up/down). I am mostly there using …

---

## [Challenge 1: height of binary tree](https://forums.kodeco.com/t/challenge-1-height-of-binary-tree/85536)

<div class="topic-metadata">

**Author:** [@sahsubodh](https://forums.kodeco.com/u/sahsubodh)\
**Replies:** 4\
**Last updated:** [January 12, 2020, 1:02am UTC](https://forums.kodeco.com/t/challenge-1-height-of-binary-tree/85536 "2020-01-12T01:02:48Z")

</div>

Can someone please tell me why does the base case has value -1 ? shouldnt it be 0? guard let node = node else{ return -1; }

---

## [Challenge 1 Stack Challenges](https://forums.kodeco.com/t/challenge-1-stack-challenges/87299)

<div class="topic-metadata">

**Author:** [@mcneils](https://forums.kodeco.com/u/mcneils)\
**Replies:** 1\
**Last updated:** [January 1, 2020, 5:33am UTC](https://forums.kodeco.com/t/challenge-1-stack-challenges/87299 "2020-01-01T05:33:10Z")

</div>

I understand how to reverse a stack, but I’m trying to use the function that we created inside the example helper function to reverse the stack that I created inside the closure, but I keep getting the error “Cannot conv…

---

## [Improvement in Radix Sort Challenge Solution](https://forums.kodeco.com/t/improvement-in-radix-sort-challenge-solution/84906)

<div class="topic-metadata">

**Author:** [@pradnya\_nikam](https://forums.kodeco.com/u/pradnya_nikam)\
**Replies:** 1\
**Last updated:** [November 14, 2019, 6:24pm UTC](https://forums.kodeco.com/t/improvement-in-radix-sort-challenge-solution/84906 "2019-11-14T18:24:54Z")

</div>

Want to suggest a small improvement in the solution for Radix Sort Challenge The challenge is to implement MSD radix sort. (Ch.31; Page: 365) The current base case checks if the position is valid for the maximum numbe…

---

## [Why is guard tail !== node else going into the scope](https://forums.kodeco.com/t/why-is-guard-tail-node-else-going-into-the-scope/87717)

<div class="topic-metadata">

**Author:** [@mcneils](https://forums.kodeco.com/u/mcneils)\
**Replies:** 1\
**Last updated:** [November 12, 2019, 2:07am UTC](https://forums.kodeco.com/t/why-is-guard-tail-node-else-going-into-the-scope/87717 "2019-11-12T02:07:47Z")

</div>

In the LinkedList section, when you are inserting a node at the tail you have the case guard tail !== node else { append(value) //add to end of the list by calling append func return } what does…

---

## [Can't find the sources directory](https://forums.kodeco.com/t/cant-find-the-sources-directory/83515)

<div class="topic-metadata">

**Author:** [@itsirak](https://forums.kodeco.com/u/itsirak)\
**Replies:** 1\
**Last updated:** [October 2, 2019, 4:14pm UTC](https://forums.kodeco.com/t/cant-find-the-sources-directory/83515 "2019-10-02T16:14:14Z")

</div>

Can anyone help find the location of the Sources directory, I cant find it in the starter project. Thanks

---

## [Heap Challenge 4 Solution bug](https://forums.kodeco.com/t/heap-challenge-4-solution-bug/66884)

<div class="topic-metadata">

**Author:** [@oaranger](https://forums.kodeco.com/u/oaranger)\
**Replies:** 4\
**Last updated:** [September 13, 2019, 3:07pm UTC](https://forums.kodeco.com/t/heap-challenge-4-solution-bug/66884 "2019-09-13T15:07:17Z")

</div>

Hello, In Heap Challenge 4 solution (check if a heap is a min heap), if we pass in elements = \[6, 8, 11, 15, 12, 20\], it will “out of range” The book solution is on page 353 I believe we did not filter out the case be…

---

## [CH 13: Binary Tree Challenge 2, \`deserialize\` function is not working](https://forums.kodeco.com/t/ch-13-binary-tree-challenge-2-deserialize-function-is-not-working/77653)

<div class="topic-metadata">

**Author:** [@yhkung](https://forums.kodeco.com/u/yhkung)\
**Replies:** 2\
**Last updated:** [August 9, 2019, 9:28am UTC](https://forums.kodeco.com/t/ch-13-binary-tree-challenge-2-deserialize-function-is-not-working/77653 "2019-08-09T09:28:00Z")

</div>

I was trying to solve challenge problems. After hours of try, I still can’t solve this. So, I peek the final project to see how this function works, But it’s not working :frowning: the deserialze function result a comp…

---

## [Tree Deserialize solution bug](https://forums.kodeco.com/t/tree-deserialize-solution-bug/70861)

<div class="topic-metadata">

**Author:** [@oaranger](https://forums.kodeco.com/u/oaranger)\
**Replies:** 2\
**Last updated:** [August 5, 2019, 7:28pm UTC](https://forums.kodeco.com/t/tree-deserialize-solution-bug/70861 "2019-08-05T19:28:47Z")

</div>

Hello, On page 339, solution for deserialize the tree need extra condition // 2 guard let value = array.removeFirst() else { should be changed to: // 2 guard !array.isEmpty, let value = array.removeFirst() else { O…

---

## [Linked List Solutions CHP 5 Problem 1 pg65](https://forums.kodeco.com/t/linked-list-solutions-chp-5-problem-1-pg65/72191)

<div class="topic-metadata">

**Author:** [@ben\_jammin](https://forums.kodeco.com/u/ben_jammin)\
**Replies:** 1\
**Last updated:** [May 28, 2019, 2:50pm UTC](https://forums.kodeco.com/t/linked-list-solutions-chp-5-problem-1-pg65/72191 "2019-05-28T14:50:50Z")

</div>

I am extremely confused with how this book is written. For the first solution it says: “add the following function to your playground” : private func printInReverse(\_ node: Node?) { // 1 guard let node = node else { re…

---

## [Need help understanding sort() function in Heap](https://forums.kodeco.com/t/need-help-understanding-sort-function-in-heap/60511)

<div class="topic-metadata">

**Author:** [@anothershitdev](https://forums.kodeco.com/u/anothershitdev)\
**Replies:** 5\
**Last updated:** [February 13, 2019, 12:17am UTC](https://forums.kodeco.com/t/need-help-understanding-sort-function-in-heap/60511 "2019-02-13T00:17:13Z")

</div>

mutating func remove() -\> Element? { guard !isEmpty else { return nil } // 1 elements.swapAt(0, elements.count-1) // 2 defer { siftDown(from: 0) // 4 …

[Previous page](https://forums.kodeco.com/c/books/data-structures-algorithms-swift/40.md?page=1)

[Next page](https://forums.kodeco.com/c/books/data-structures-algorithms-swift/40.md?page=3)
