Data Structures & Algorithms Basics


Data Structures & Algorithms Basics

Anna
Hey Michael, ever wondered why data structures and algorithms are such a big deal in coding interviews?

Michael
All the time! And apparently companies like Google and Amazon use them to see if you think like a programmer. But it feels like there's so much jargon—Big O, arrays, linked lists… where do we even start?

Anna
Let's start with Big O. It's basically a way to describe how fast or slow an algorithm is as the input gets huge. Think of it as a performance rating. If your code runs quickly on your laptop but slows to a crawl when you give it a million records, that's what Big O helps you predict.

Michael
So it's about scalability. Got it. But the formal definition sounds scary—'limiting behavior of a function when the argument tends towards infinity.'

Anna
Don't let that intimidate you. The core idea is simple: we measure how the runtime grows relative to the input size. For example, if an algorithm always takes the same time regardless of input, we call that O(1)—constant time.

Michael
Right. Like accessing an array element by index: it's one step, no matter how big the array is. That's O(1).

Anna
Exactly. Now imagine a loop that prints every item in an array. If the array has n items, you do n prints. That's linear time—O(n). The runtime grows proportionally with the input.

Michael
And if you have two loops back-to-back over the same array? That's O(2n), but we drop the constant and still call it O(n). Because both are linear—the important part is that the growth is still proportional.

Anna
Right. We always simplify Big O to the dominant term. Nested loops, on the other hand, multiply. A loop inside another loop over the same array gives O(n²)—quadratic time. That gets slow fast.

Michael
And if you have three nested loops, it's O(n³). Exponential growth? That's the opposite of logarithmic, right? Where the algorithm slows down as input grows?

Anna
Exactly! Logarithmic time—O(log n)—is what makes binary search so efficient. With a sorted array, you keep splitting the search space in half. With a million items, you need only about 19 comparisons. That's the power of cutting the problem in half each step.

Michael
That's amazing. And exponential time—O(2^n)—is the opposite: it doubles with each new element, becoming unusable quickly. But let's talk about space complexity too. Big O isn't just about time.

Anna
True. Sometimes you trade space for time. For example, if you create a copy of an array that's the same size as the input, that's O(n) additional space. On a mobile device with limited memory, you might need to optimize for space instead.

Michael
Makes sense. Now, let's look at arrays themselves. They're simple: a sequence of items stored contiguously in memory. That's why index lookup is O(1)—just compute the address.

Anna
But arrays have a fixed size. In Java, if you need to add more items than the allocated size, you have to create a new larger array and copy everything over. That copying is O(n). And inserting or deleting in the middle requires shifting elements—also O(n) in the worst case.

Michael
So dynamic arrays like ArrayList handle that by automatically resizing—usually by 50% or 100%—but the cost is still there. That's why linked lists exist.

Anna
Linked lists solve the resizing problem. They're made of nodes, each holding a value and a pointer to the next node. They grow and shrink without copying everything.

Michael
But they have their own trade-offs. Lookup by index is O(n) because you have to traverse from the head. And deleting the last node requires finding the previous node, which is also O(n) unless you have a doubly linked list.

Anna
Exactly. But inserting at the beginning or end is O(1) if you have a reference to head and tail. That's a big win over arrays for operations that modify the list frequently.

Michael
So let's recap the operations on a linked list: adding at the front or back is O(1), removing from the front is O(1), but removing from the back is O(n) because you need the previous node. Searching and indexing are O(n).

Anna
And remember, when we analyze, we always consider the worst-case scenario. For example, linear search in an array is O(n) because the item might be at the end.

Michael
That's a key principle: Big O describes the worst-case growth rate. Now, I've seen how to build a dynamic array from scratch. Let's walk through the steps for a custom Array class.

Anna
Sure! First, you need a constructor that takes an initial capacity and allocates a regular int array. Keep a 'count' variable to track how many items are actually stored.

Michael
Then the insert method: if the array is full (count equals length), you create a new array twice the size, copy all items over using a loop, and then assign the new array to the old reference. Then add the new item at index 'count' and increment count.

Anna
For removeAt, first validate the index—throw an exception if it's out of range. Then, shift all elements to the left from that index to the end, and decrement count. That way, you effectively remove the item.

Michael
And indexOf: loop through from index 0 to count-1, compare each element, and return the index if found; otherwise return -1.

Anna
A print method can simply loop from 0 to count and print the items. But remember, in a real design, the array class shouldn't handle printing—that's a separate concern. But for learning, it's okay.

Michael
Now for a custom LinkedList. We need a Node class—it has a value and a next pointer. The LinkedList has first and last fields (head and tail).

Anna
AddLast: create a new node. If the list is empty, set first and last to it. Otherwise, set last.next to the node and update last. AddFirst is similar but updates the head.

Michael
RemoveFirst: if the list is empty, throw an exception. If there's only one node, set both first and last to null. Otherwise, store first.next in a temporary variable, set first.next to null to remove the link, then set first to that temporary.

Anna
RemoveLast is trickier. You need to find the node before the last. You can create a helper method 'getPrevious' that traverses from the head until it finds a node whose next is the target. Then update last to that previous node and set its next to null.

Michael
Edge cases matter: what if the list is empty? What if it has a single node? Always check those.

Anna
Exactly. And contains can just use indexOf: if indexOf returns -1, it's false, else true.

Michael
So we've covered the basics: Big O notation to analyze time and space complexity, arrays and their limitations, and linked lists as a more flexible alternative. Both are foundational for understanding more complex structures like stacks, queues, and trees.

Anna
And remember, knowing these concepts not only helps you ace interviews but also makes you a better developer. You'll naturally think about performance when choosing data structures.

Michael
Well said. Now I feel ready to dive deeper into stacks and queues. Thanks for the great chat, Anna!

Ask anything
We've read this whole study set, so questions about it get the best answers.