Posts

Showing posts from October, 2022

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...

DSA GRIND #4

Image
 DSA GRIND #4 BY ASHUR BAROUTTA This week we kept up with the trend of solving 2 problems a week. The first problem asked us to concatenate two arrays. Stating "Given an integer array nums of length N, create an array ans of length 2N where ans[i] == nums[i] and ans[i+n] == nums[i]". This took more thought than actual coding as the solution turned out to be far simpler than imagined.  I settled for making a new array with twice the size of nums length, then I initialized a variable j = 0. After that I made a for loop and within that loop I set a condition to check if i was less than the length of the original array as if it was I knew I could just make the indexes of the new array equal to the same index as nums array. After that, we set newarray[i] equal to nums[j] as j is incrementing from 0 and at the point where this started we would be passed all the first recorded indexes.  A merger like this is probably useful when wanting to join two different specific arrays into...

Building A Simple Program with C (DiceGame)

Image
 DiceGame by Ashur Baroutta     Recently I've been learning the basic's of a language new to me, called C. It is actually quite old though the way in which you code with it looks similar to Java. I was tasked with creating a dice game that featured two players rolling until one of them had accumulated 100 or more points.  The rules for the game are simple, a player rolls 2 dice, if one of the dice is a 1 then the player records a 0 for the round. A player can choose to stand on their points accumulated that round to avoid the risk of rolling a 1 and scoring 0 for the turn. The first to reach a hundred or more wins. There are modifiers for specific scenarios, if a player should roll snake eyes (1 and 1) they score 25 points for that roll and if a player should roll two of the same number, that sum is doubled.      In theory, this game shouldn't have been so difficult to code though the way C works isn't the same as modern languages as it uses pointers t...