Posts

DSA GRIND #11

Image
                DSA GRIND #11 - Ashur Baroutta        This week we spent a lot of time attacking a problem that required us to look at a specific "side" of a binary tree. Our problem statement requests we imagine we are standing on the right side of a binary tree and returning the numbers we see from top to bottom.       This had me stumped for a good while, I knew how to traverse a Binary tree. I knew how to check for values greater than each other for a given node and leaf.  However, only checking and comparing right-side nodes with each other and then printing from the top down was a whole different game for me.       In the end, I managed but not before spending more time on this than any project I've done to this point this semester. Glad I got through it, I'm trying to find different usecases for this sort of application but maybe its biggest benefit was just challenging m...

DSA GRIND #9

Image
 INVERTING A BINARY TREE - AB  This weeks problem is similar to our earlier work on reversing a linked list data structure, but in this instance we're going to be inverting a binary tree. The following picture shows an example of what that means. Like our earlier work, I went about doing this recursively as well. So kept making calls back to the function within itself as we went about inverting the data structure node for node. The results/code is as follows.  The code was solid, though the memory management was pretty lacking, its resource intensive holding the same calls within the stack for execution. 

DSA GRIND #8

Image
 Kth Largest Element in the Stream : by A.B      We're back at it again this week and focusing on a problem involving the heap data structure. For those unfamiliar, a Heap is a tree-based data structure that is a complete binary tree in that for a given node B, if A is the "parent" node of B, the value of A is greater/equal to the value in B.     So our problem description and an example to help understand it is provided below.      I do have some experience with binary trees but this solution required way more work than I expected. The challenging part was not being familiar with methods unique to heap structures. It took a big of googling/studying to understand what I actually had to do in order to get my code to perform the way I had it lined up in my mind. In any case, I did what I sought to do and got a submission to pass all test cases. Admittedly, I'm ignorant to if I can optimize this better, I'll be looking into other solutions to se...

DSA GRIND #7

Image
REVERSING LINKED LIST(RECURSIVELY)     The grind continues! This week we're staying on linked lists as I've been working with them a lot more given my coding class this semester. Reversing a linked list is asked commonly in coding interviews, it's more of a beginner question and I wanted to tackle it with recursion.      The problem statement is very simple "Given the head of a singly linked list, reverse the list". At first, I initialized a bunch of variables to continuously swap as I traversed the link list, and while the spirit of that approach isn't wrong, I thought it more of a challenge to think through how to do this recursively.  So I setup parameterized functions to compute the reversal recursively utilizing a second helper function. This was really fun! The code and solution is posted below. 

DSA GRIND #6

Image
DSA GRIND by Ashur Baroutta         This week's problem is a classic! I wanted to tackle another Binary Search problem. So our problem statement reads -      " Given an array of integers  nums  which is sorted in ascending order, and an integer  target , write a function to search  target  in  nums . If  target  exists, then return its index. Otherwise, return  -1 ."     So after spending a bit of time trying to figure out how to do this i settled on using a twopointer combined with a while loop setup and setting a variable to target the middle of the array and comparing our index of mid to our target and incrementing/decrementing based off the result of the comparisons. I can see why its a classic. While it may not be as widespread now, I think this type of search is useful in returning specific data from a smaller dataset. It probably doesn't scale too well for super large datasets.

DSA #5

Image
 DSA GRIND #5 - ASHUR BAROUTTA     This week I chose to touch back on a linkedlist problem as I find that to be an interesting data structure. I think the problem is cool, it states  " Given   head , the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the  next  pointer. Internally,  pos  is used to denote the index of the node that tail's  next  pointer is connected to." So the problem is asking us to traverse the linked list in a specific way to see if there is a cycle. At first I thought this problem wouldn't take me much time but it ended up being alot more challenging than going from node to node and checking what was next.  I ended up using two different pointers to traverse the list at different "speeds". Was proud of this one. The results are okay in terms of rank, but im st...