# Data Structures

**URL:** https://forum.codewithmosh.com/c/data-structures/14.md

[Latest](https://forum.codewithmosh.com/latest.md) · [Categories](https://forum.codewithmosh.com/categories.md)

---

## [About the Data Structures category](https://forum.codewithmosh.com/t/about-the-data-structures-category/25)

<div class="topic-metadata">

**Author:** [@Mosh](https://forum.codewithmosh.com/u/Mosh)\
**Replies:** 0\
**Last updated:** [October 22, 2020, 10:29pm UTC](https://forum.codewithmosh.com/t/about-the-data-structures-category/25 "2020-10-22T22:29:03Z")

</div>

---

## [Clarification on O(1) vs O(2) vs O(n) vs O(n^2) vs O(log n) vs O(2^n)](https://forum.codewithmosh.com/t/clarification-on-o-1-vs-o-2-vs-o-n-vs-o-n-2-vs-o-log-n-vs-o-2-n/72092)

<div class="topic-metadata">

**Author:** [@King\_Sardius](https://forum.codewithmosh.com/u/King_Sardius)\
**Replies:** 0\
**Last updated:** [January 4, 2026, 11:47pm UTC](https://forum.codewithmosh.com/t/clarification-on-o-1-vs-o-2-vs-o-n-vs-o-n-2-vs-o-log-n-vs-o-2-n/72092 "2026-01-04T23:47:16Z")

</div>

The focus of the “Big O” notation is not the operations of standard methods/functions on a program, but rather the exponential growth of operations demanded by new input. Thus , it is not how many methods are running in …

---

## [AVL tree .isPerfect()](https://forum.codewithmosh.com/t/avl-tree-isperfect/71107)

<div class="topic-metadata">

**Author:** [@techabie](https://forum.codewithmosh.com/u/techabie)\
**Replies:** 0\
**Last updated:** [February 6, 2025, 8:02pm UTC](https://forum.codewithmosh.com/t/avl-tree-isperfect/71107 "2025-02-06T20:02:12Z")

</div>

I can’t find the solution in anywhere on the AVLtree.java in /src folder. Can anyone show me the complete answer for that? My best guess now is to compare the number of leaf node of the tree to the max number of leaf it…

---

## [Learn java for Data structures and algorithms?](https://forum.codewithmosh.com/t/learn-java-for-data-structures-and-algorithms/28075)

<div class="topic-metadata">

**Author:** [@theorion](https://forum.codewithmosh.com/u/theorion)\
**Replies:** 2\
**Last updated:** [August 7, 2024, 3:31pm UTC](https://forum.codewithmosh.com/t/learn-java-for-data-structures-and-algorithms/28075 "2024-08-07T15:31:48Z")

</div>

I learned all the frontend and backend courses with mosh on the Javascript course and now I’m doing the DS&A course but mosh does it with java and i’m not familiar. He said it’s okay to do with any language, but i’m fin…

---

## [BucketSort implementation is not completely correct](https://forum.codewithmosh.com/t/bucketsort-implementation-is-not-completely-correct/14301)

<div class="topic-metadata">

**Author:** [@satthecoder](https://forum.codewithmosh.com/u/satthecoder)\
**Replies:** 7\
**Last updated:** [July 27, 2024, 7:11pm UTC](https://forum.codewithmosh.com/t/bucketsort-implementation-is-not-completely-correct/14301 "2024-07-27T19:11:27Z")

</div>

I’ve tested the code in the video, but when you add larger numbers like 888 to the array and test your bucketSort class, there might be out of scope error. Because 888/3 = 296 and you don’t have any bucket at index 296 o…

---

## [HashTable possible bug on GetOrCreateBucket](https://forum.codewithmosh.com/t/hashtable-possible-bug-on-getorcreatebucket/27556)

<div class="topic-metadata">

**Author:** [@dimanjenje](https://forum.codewithmosh.com/u/dimanjenje)\
**Replies:** 0\
**Last updated:** [July 9, 2024, 11:37am UTC](https://forum.codewithmosh.com/t/hashtable-possible-bug-on-getorcreatebucket/27556 "2024-07-09T11:37:45Z")

</div>

I finished the Part 1 of the course, by using C# (with a Unit Test Project in the same solution), I think there’s a bug method GetOrCreateBucket(key), in HashTable video Solution - Refactoring… In case the bucket is …

---

## [Interesting detail about HashMap's Linear Probing (and a bug!)](https://forum.codewithmosh.com/t/interesting-detail-about-hashmaps-linear-probing-and-a-bug/11727)

<div class="topic-metadata">

**Author:** [@gecko](https://forum.codewithmosh.com/u/gecko)\
**Replies:** 3\
**Last updated:** [July 5, 2024, 11:39am UTC](https://forum.codewithmosh.com/t/interesting-detail-about-hashmaps-linear-probing-and-a-bug/11727 "2024-07-05T11:39:36Z")

</div>

I was confused as to how linear probing as described (for handing hash function collisions) could work. Consider the following for a hash function of (key % length) with length 10: hashmap.put(4, "Blue"); hashmap.put(5…

---

## [Solving coding problems](https://forum.codewithmosh.com/t/solving-coding-problems/27161)

<div class="topic-metadata">

**Author:** [@a.madhu](https://forum.codewithmosh.com/u/a.madhu)\
**Replies:** 1\
**Last updated:** [June 13, 2024, 11:32pm UTC](https://forum.codewithmosh.com/t/solving-coding-problems/27161 "2024-06-13T23:32:24Z")

</div>

I completed data structures and algorithms in your website. But I fully struggle to solve leetcode coding problems. There are so many algorithms like two pointers, sliding window, sub string, greedy algorithms, bit mani…

---

## [DataStructures - Hash Tables - 5.Video - Missing (count + 1)](https://forum.codewithmosh.com/t/datastructures-hash-tables-5-video-missing-count-1/25490)

<div class="topic-metadata">

**Author:** [@samet](https://forum.codewithmosh.com/u/samet)\
**Replies:** 0\
**Last updated:** [March 6, 2024, 11:07am UTC](https://forum.codewithmosh.com/t/datastructures-hash-tables-5-video-missing-count-1/25490 "2024-03-06T11:07:20Z")

</div>

5- Solution- First Non-repeating Character - 04:33 I think in line 13 has to be like “map.put( ch , count + 1);”

---

## [Array Exercise Solutions](https://forum.codewithmosh.com/t/array-exercise-solutions/1217)

<div class="topic-metadata">

**Author:** [@kan15hk](https://forum.codewithmosh.com/u/kan15hk)\
**Replies:** 3\
**Last updated:** [February 23, 2024, 10:49am UTC](https://forum.codewithmosh.com/t/array-exercise-solutions/1217 "2024-02-23T10:49:42Z")

</div>

Is there some place where we can find the Array Exercise Solutions? I know there is a line with the function that is the solution but I would also like to compare my code with someone else’s and discuss time complexity f…

---

## [Looking for a sanity check on validating binary search trees](https://forum.codewithmosh.com/t/looking-for-a-sanity-check-on-validating-binary-search-trees/3021)

<div class="topic-metadata">

**Author:** [@Vikeman](https://forum.codewithmosh.com/u/Vikeman)\
**Replies:** 4\
**Last updated:** [February 21, 2024, 10:18pm UTC](https://forum.codewithmosh.com/t/looking-for-a-sanity-check-on-validating-binary-search-trees/3021 "2024-02-21T22:18:06Z")

</div>

I’m not sure if this is an error by @programmingwithmosh or my understanding is flawed. In Mosh’s solution for validating binary search trees (lecture 16 of Binary Trees topic in The Ultimate Data Structures & Algorithms…

---

## [Visualization of a Radix Tree and Patricia Tree](https://forum.codewithmosh.com/t/visualization-of-a-radix-tree-and-patricia-tree/24784)

<div class="topic-metadata">

**Author:** [@abcdefghi](https://forum.codewithmosh.com/u/abcdefghi)\
**Replies:** 0\
**Last updated:** [January 26, 2024, 4:43pm UTC](https://forum.codewithmosh.com/t/visualization-of-a-radix-tree-and-patricia-tree/24784 "2024-01-26T16:43:49Z")

</div>

Hi all, I am trying to create trees, but are unsure if they are correct. Can you please take a look and correct me if there are any mistakes. I want to visualize the data structure RadixTree and Patricia Trie using the…

---

## [LinkedList: Why would there be a loop?](https://forum.codewithmosh.com/t/linkedlist-why-would-there-be-a-loop/11360)

<div class="topic-metadata">

**Author:** [@isimmons33](https://forum.codewithmosh.com/u/isimmons33)\
**Replies:** 2\
**Last updated:** [December 4, 2023, 7:50am UTC](https://forum.codewithmosh.com/t/linkedlist-why-would-there-be-a-loop/11360 "2023-12-04T07:50:06Z")

</div>

I was wondering in what situations could a linked list have a loop? To test the hasLoop() method we have to intentionally create a LinkedList with a loop (basically make a mistake in our linking). The end user of this c…

---

## [Question about binary tree min and height](https://forum.codewithmosh.com/t/question-about-binary-tree-min-and-height/6493)

<div class="topic-metadata">

**Author:** [@pbotsaris](https://forum.codewithmosh.com/u/pbotsaris)\
**Replies:** 15\
**Last updated:** [October 6, 2023, 10:20pm UTC](https://forum.codewithmosh.com/t/question-about-binary-tree-min-and-height/6493 "2023-10-06T22:20:35Z")

</div>

Hello everyone, I have been following the data structure course and I notice in lesson 12(find tree min) the base case \`if(root.leftNode == null && root.rightNode == null) return root.value; does not stop the recu…

---

## [Graph Traverse DFS Recursive vs Iterative different sequence in the results](https://forum.codewithmosh.com/t/graph-traverse-dfs-recursive-vs-iterative-different-sequence-in-the-results/1356)

<div class="topic-metadata">

**Author:** [@adbarani](https://forum.codewithmosh.com/u/adbarani)\
**Replies:** 2\
**Last updated:** [August 9, 2023, 1:47am UTC](https://forum.codewithmosh.com/t/graph-traverse-dfs-recursive-vs-iterative-different-sequence-in-the-results/1356 "2023-08-09T01:47:13Z")

</div>

Going through both videos of different approach to solve DFS I’ve notice the output sequence is different They should be the same sequence since it’s just implementation details Please advise why it’s show different…

---

## [Heap Remove Issue](https://forum.codewithmosh.com/t/heap-remove-issue/21296)

<div class="topic-metadata">

**Author:** [@zalogarciam](https://forum.codewithmosh.com/u/zalogarciam)\
**Replies:** 0\
**Last updated:** [July 6, 2023, 1:55am UTC](https://forum.codewithmosh.com/t/heap-remove-issue/21296 "2023-07-06T01:55:33Z")

</div>

Hello, I am trying to translate the Java Heap code to Python, but not sure why its not working when I remove an item from my list. I think the logic is the right one. import math class Heap: def \_\_init\_\_(self) -\> N…

---

## [Run time complexity for inserting a node in the Linked List at the end](https://forum.codewithmosh.com/t/run-time-complexity-for-inserting-a-node-in-the-linked-list-at-the-end/21258)

<div class="topic-metadata">

**Author:** [@Karnasinh](https://forum.codewithmosh.com/u/Karnasinh)\
**Replies:** 0\
**Last updated:** [July 4, 2023, 2:20pm UTC](https://forum.codewithmosh.com/t/run-time-complexity-for-inserting-a-node-in-the-linked-list-at-the-end/21258 "2023-07-04T14:20:25Z")

</div>

Hello Everyone, In the Data Structure Course for Linked List topic there is one mistake on “2-What are linked list (4:22)” video at 1:55 time stamp. For the Linked List if we want to insert the node at the end then run…

---

## [Bug in tree height](https://forum.codewithmosh.com/t/bug-in-tree-height/20589)

<div class="topic-metadata">

**Author:** [@susahin](https://forum.codewithmosh.com/u/susahin)\
**Replies:** 0\
**Last updated:** [June 1, 2023, 12:23pm UTC](https://forum.codewithmosh.com/t/bug-in-tree-height/20589 "2023-06-01T12:23:08Z")

</div>

There is a bug in finding the tree height. If we create a tree with these items: var tree = new Tree(); tree.insert(7); tree.insert(4); tree.insert(9); tree.insert(1); tre…

---

## [Dynamic Programming](https://forum.codewithmosh.com/t/dynamic-programming/20529)

<div class="topic-metadata">

**Author:** [@tcaniagboso](https://forum.codewithmosh.com/u/tcaniagboso)\
**Replies:** 1\
**Last updated:** [May 30, 2023, 4:25pm UTC](https://forum.codewithmosh.com/t/dynamic-programming/20529 "2023-05-30T16:25:13Z")

</div>

Is there any course that teaches Dynamic Programming? I am currently taking the Data Structures and Algorithm course and I don’t see a Dynamic Programming subsection

---

## [Directed Graph detecting cycle algorithm](https://forum.codewithmosh.com/t/directed-graph-detecting-cycle-algorithm/20286)

<div class="topic-metadata">

**Author:** [@rob\_marques](https://forum.codewithmosh.com/u/rob_marques)\
**Replies:** 0\
**Last updated:** [May 16, 2023, 6:10pm UTC](https://forum.codewithmosh.com/t/directed-graph-detecting-cycle-algorithm/20286 "2023-05-16T18:10:53Z")

</div>

Hey! I’m following part 2 of the course but some things do not make sense to me, for example: When we’re talking about detecting a cycle algorithm in a directed graph, why do we need the set “all”? We already have all t…

---

## [Linked lists: Reversing a linked list](https://forum.codewithmosh.com/t/linked-lists-reversing-a-linked-list/1175)

<div class="topic-metadata">

**Author:** [@Kalhari](https://forum.codewithmosh.com/u/Kalhari)\
**Replies:** 4\
**Last updated:** [April 27, 2023, 6:07am UTC](https://forum.codewithmosh.com/t/linked-lists-reversing-a-linked-list/1175 "2023-04-27T06:07:38Z")

</div>

A recursive solution to the linked list reversion :slight\_smile:

---

## [Where can I find the solution to exercises within the course?](https://forum.codewithmosh.com/t/where-can-i-find-the-solution-to-exercises-within-the-course/19515)

<div class="topic-metadata">

**Author:** [@nevy](https://forum.codewithmosh.com/u/nevy)\
**Replies:** 2\
**Last updated:** [April 27, 2023, 5:22am UTC](https://forum.codewithmosh.com/t/where-can-i-find-the-solution-to-exercises-within-the-course/19515 "2023-04-27T05:22:20Z")

</div>

For example in the Stack section, it says the solution is Twostacks and MinStack. Is there a link where we can access the code of these solutions?

---

## [Space complexity](https://forum.codewithmosh.com/t/space-complexity/18609)

<div class="topic-metadata">

**Author:** [@DSSagarKisan](https://forum.codewithmosh.com/u/DSSagarKisan)\
**Replies:** 1\
**Last updated:** [April 27, 2023, 5:20am UTC](https://forum.codewithmosh.com/t/space-complexity/18609 "2023-04-27T05:20:20Z")

</div>

In calculating space complexity, we don’t consider the input size. we only consider the extra space allocated. so my question is Do we consider output space while calculating space complexity? For example, let’s conside…

---

## [Linked List reversing. Is my method correct or unacceptable?](https://forum.codewithmosh.com/t/linked-list-reversing-is-my-method-correct-or-unacceptable/19437)

<div class="topic-metadata">

**Author:** [@nevy](https://forum.codewithmosh.com/u/nevy)\
**Replies:** 1\
**Last updated:** [April 7, 2023, 11:34pm UTC](https://forum.codewithmosh.com/t/linked-list-reversing-is-my-method-correct-or-unacceptable/19437 "2023-04-07T23:34:50Z")

</div>

public void reverse(){ var current = first; int array = new int\[size\]; int index = 0; while (current != null){ array\[index++\] = current.value; current = current.next; } index–; current = first; while (current !…

---

## [Datas Structures and Algorithms: Depth First Traversal With Recursion (Python)](https://forum.codewithmosh.com/t/datas-structures-and-algorithms-depth-first-traversal-with-recursion-python/17017)

<div class="topic-metadata">

**Author:** [@sharktank](https://forum.codewithmosh.com/u/sharktank)\
**Replies:** 3\
**Last updated:** [February 2, 2023, 6:06am UTC](https://forum.codewithmosh.com/t/datas-structures-and-algorithms-depth-first-traversal-with-recursion-python/17017 "2023-02-02T06:06:48Z")

</div>

So I’m on part 10 of the DS & A binary tree video where you actually create a depth-first traversal function using recursion I’m doing it in python. Right now, this is the code I have for the function but when I run it,…

---

## [Heapify function bug?](https://forum.codewithmosh.com/t/heapify-function-bug/3116)

<div class="topic-metadata">

**Author:** [@Kasia](https://forum.codewithmosh.com/u/Kasia)\
**Replies:** 6\
**Last updated:** [January 23, 2023, 1:59pm UTC](https://forum.codewithmosh.com/t/heapify-function-bug/3116 "2023-01-23T13:59:36Z")

</div>

I think there is a bug in the implementation of the heapify function. When the values are swapped there may be a case when the value of the child is greater than the parent. But the heapify recursion never goes back to p…

---

## [Refund for the course I purchased within 30 days](https://forum.codewithmosh.com/t/refund-for-the-course-i-purchased-within-30-days/17258)

<div class="topic-metadata">

**Author:** [@Harsh](https://forum.codewithmosh.com/u/Harsh)\
**Replies:** 1\
**Last updated:** [December 30, 2022, 6:51am UTC](https://forum.codewithmosh.com/t/refund-for-the-course-i-purchased-within-30-days/17258 "2022-12-30T06:51:15Z")

</div>

The course I purchased is not very basic of me “Ds and algo” I want refund @codewithmosh so that I can purchase different course

---

## [Intersection() method](https://forum.codewithmosh.com/t/intersection-method/16700)

<div class="topic-metadata">

**Author:** [@Raimon](https://forum.codewithmosh.com/u/Raimon)\
**Replies:** 3\
**Last updated:** [November 28, 2022, 9:55am UTC](https://forum.codewithmosh.com/t/intersection-method/16700 "2022-11-28T09:55:10Z")

</div>

I have the same question as Jack007 at page: There was a solution given but I don’t understand. Can someone explain it to me? This was part of the exercizes in the Arrays module lesson 10: https://codewithmosh.com/c…

---

## [Got error in when removeAt() function in java](https://forum.codewithmosh.com/t/got-error-in-when-removeat-function-in-java/16066)

<div class="topic-metadata">

**Author:** [@Harif07](https://forum.codewithmosh.com/u/Harif07)\
**Replies:** 5\
**Last updated:** [October 28, 2022, 3:28am UTC](https://forum.codewithmosh.com/t/got-error-in-when-removeat-function-in-java/16066 "2022-10-28T03:28:41Z")

</div>

Hello guys, I got error when Implement removeAt() function in arryas (ds&algo part1). I coded same as mosh how taught in video. But it got error like (index 6 out of bounds for length 6) Please give any solution for th…

---

## [Potential error in implementing MinStack](https://forum.codewithmosh.com/t/potential-error-in-implementing-minstack/14833)

<div class="topic-metadata">

**Author:** [@Suki](https://forum.codewithmosh.com/u/Suki)\
**Replies:** 1\
**Last updated:** [August 31, 2022, 5:54am UTC](https://forum.codewithmosh.com/t/potential-error-in-implementing-minstack/14833 "2022-08-31T05:54:12Z")

</div>

Hi everyone, I think there might be a potential error in the solution of implementing the MinStack given by Mosh. The line of code ‘item \< minStack.peek()’ should actually be ‘item \<= minStack.peek()’. Try this example…

[Next page](https://forum.codewithmosh.com/c/data-structures/14.md?page=1)
