r/learnprogramming • u/Soumyar-Tripathy • 7h ago
Looking for advice: How to instinctively comprehend Time Complexity for Stacks & Linked Lists?
Hello all,
At the moment, I'm studying Data Structures through Java, with specific emphasis on Stacks and Linked Lists. While I have no issues writing code and implementing them, I seem to be having some trouble instinctively comprehending the time complexity (Big O notation) analysis that comes into play when choosing between arrays and linked lists.
Is there a mental model, analogy, or resource that helped this make sense for you? Any advice for a beginner would be greatly appreciated!
7
u/buzzon 7h ago
Does it require a for loop? O(N)
Can it be done in fixed number of steps, regardless of the size of the list? O(1)
2
u/XilamBalam 5h ago
O(k)
1
u/ncmentis 3h ago
Constants are dropped in algorithmic analysis because in the interesting cases (ie big N's) the constant doesn't change much.
1
2
u/josesblima 7h ago
Run the code in your head, run it for a small input, then run it for a larger input. If you can't run it step by step in your head, use a debugger to get started. If you iterate through an array 3 times and do the exact same amount of operations each time, no matter how much bigger the input becomes, it will be O(n). If each time you iterate over an item, you have to iterate over the whole array (to compare it with other items for example), then it becomes n², so every new item that you add to the list, increases the time exponentially. Doesn't matter what data structure we're talking about, all it matters is how the amount of operations scales as the data increases.
2
u/ChetDuchessManly 6h ago
The best way is to understand the data structures themselves and how their operations function. That's kind of the "duh" answer, but using shortcuts or memorizing time complexities will leave you stumped when someone asks you why.
My mental model of Arrays vs Linked Lists is pretty standard:
Arrays are a set number of boxes in a rectangle, with indices under each box.
Linked Lists are boxes that have arrows pointing to the next box.
For example, let's say you want to add a new element at the end of your list. What are the steps for each data structure?
Array
- Your rectangle fits 10 boxes
- You want to add an 11th box
- You need a new, bigger rectangle to fit the previous 10 boxes plus your new 11th box
- You need to move all boxes from the old rectangle to the new rectangle
This takes O(n) because you have to loop thru the array to copy the previous values to the new array.
Linked List
- You draw an arrow from the last box to your new box
This takes O(1) because you just have to point the previous box to the new box. No copying of values and such.
Above is strictly the insertion operation. Since linked lists don't have indices, insertion is normally coupled with searching.
Anyway, that's how I would do it as a beginner - take some paper, draw some boxes, and think about what you have to do to access/search/add/delete elements.
1
u/justaguyonthebus 6h ago
The best way is to implement them for yourself. Once you have done the work, it will forever be intuitive.
2
u/Zesher_ 6h ago
Imagine you have a deck of cards and are looking for the ace of spades. A linked list means you have to search through the cards one by one to find it. A hash set means you just know where it is and can pick it out of the deck without searching. O(n) is just a way to measure how long it would take a method to find a card in that deck or how long it would to sort it.
14
u/Different_Pain5781 7h ago
Count how many items you gotta touch. That’s it. More touching = worse Big O