| Method | Time Complexity | Reason |
|---|---|---|
sum() | O(1) | Simple arithmetic + return |
array() | O(n) | Because of loop through array |
| Array index | O(1) | Direct access by index |
constant() | O(1) | Single assignment |
Github code sample O(1)
String[] names = {"Alex", "Brian", "Cathy", ..., "Zoe"};
for (String name : names) {
System.out.println(name); // Print each name
}
If the list has 100 names, we print 100 lines.
✅ If it has 1,000 names, we print 1,000 lines.
That’s O(n) behavior.
Github code sample O(n)
Github code sample O(n2)
Github code sample O(n2)
| Data Structures | Space Complexity | Average Case Time Complexity | |||
| Access | Search | Insertion | Deletion | ||
| Array | O(n) | O(1) | O(n) | O(n) | O(n) |
| Stack | O(n) | O(n) | O(n) | O(1) | O(1) |
| Queue | O(n) | O(n) | O(n) | O(1) | O(1) |
| Singly Linked List | O(n) | O(n) | O(n) | O(1) | O(1) |
| Doubly Linked List | O(n) | O(n) | O(n) | O(1) | O(1) |
| Hash Table | O(n) | N/A | O(1) | O(1) | O(1) |
| Binary Search Tree | O(n) | O(log n) | O(log n) | O(log n) | O(log n) |
| Operation | What it Means | Time (Big O) | Why |
|---|---|---|---|
| ➕ Add at the beginning | Add a new item at the front | O(1) | Just link the new item to the head of the list — super fast! |
| ❌ Delete the first item | Remove the first node | O(1) | Just move the "head" pointer to the next node |
| ➕ Add at the end | Add an item at the end | O(n) | You need to go through the whole list to find the end first |
| 🔍 Find an item | Search for something | O(n) | You might have to check every node until you find it |
| ❌ Delete random item | Remove an item from the middle | O(n) | First you have to search for it, then remove it |
| Operation | What it Means | Time (Big O) | Why |
|---|---|---|---|
push() | Add an item on top | O(1) | It goes directly to the top — no searching needed |
pop() | Remove the top item | O(1) | Just remove the item on top |
isEmpty() / isFull() | Check if stack is empty or full | O(1) | Just a quick check — no need to go through items |
size() | Get number of items | O(1) | If you keep a counter variable, it’s instant |
| Operation | What It Means | Time (Big O) | Why |
|---|---|---|---|
enqueue() | Add an item to the back of the queue | O(1) | Just place it at the end — no searching needed |
dequeue() | Remove an item from the front | O(1) | Always remove the first item |
isEmpty() / isFull() | Check if the queue is empty or full | O(1) | Quick check — no scanning required |
| Space Used | Memory needed for all items | O(n) | Because it holds n items |
Splitting (log n) × Work per level (n) = O(n log n)