Skip to main content

Posts

Showing posts with the label algorithms

Daily Coding Problem: Implement integer exponentiation (w Recursion | Bitwise)

My solution to a “Daily Coding Problem” that I received in my mail today. Implement integer exponentiation. That is, implement the  pow(x, y)  function, where  x  and  y  are integers and returns  x^y . Do this faster than the naive method of repeated multiplication. For example,  pow(2, 10)  should return 1024. Here’s my solution in Typescript. I will be honest, I can’t fully visualise the iterative solution in my head. So while I solved this, not sure how will I solve the next problem that needs some bit-shifting. It’s been some time so, I need some practice with my bitwise operations //iterartive and more efficient solution space-wise sixtyOne(x: number, y: number) { //function to calculate power let res = 1; while (y> 0) { //if y is odd multiply, //x with result if((y & 1) == 1) { res = res * x; } //n must be even now y = y >> 1; x = x...

Daily Coding Problem: Find the majority element in an array

My solution to a “Daily Coding Problem” that I received in my mail today. Given a list of elements, find the majority element, which appears more than half the time ( > floor(len(lst) / 2.0) ). You can assume that such element exists. For example, given  [1, 2, 1, 1, 3, 4, 0] , return 1. Here’s my solution in, oneFiftyFive(arr: number[]):number { if (arr == null || arr == undefined) { return 0; } let half = Math.floor(arr.length / 2); //find max occurance let occurenceMap = new Map<number, number>(); let maxOccurence = 0; for(let i = 0; i < arr.length; i++) { let n = arr[i]; if(occurenceMap.has(n)) { let val = occurenceMap.get(n) + 1; //we get out the moment we find the max occurence //what if 2 numbers have the same frequency of occurence? if(val >= half) { maxOccurence = val; break; } occurenceMap.set(n, val); } else { occurenceMap.set(n, 1); } } return max...

Daily Coding Problem: Sublist sum

My solution to a “Daily Coding Problem” that I received in my mail today. Given a list of numbers  L , implement a method  sum(i, j)  which returns the sum from the sublist  L[i:j]  (including  i , excluding  j ). For example, given  L = [1, 2, 3, 4, 5] ,  sum(1, 3)  should return  sum([2, 3]) , which is  5 . You can assume that you can do some pre-processing.  sum()  should be optimized over the pre-processing step. Here’s my solution in Typescript, oneFortyNine(l: number[], i: number,j: number): number { if(l == null) { return 0; } if(l.length == 0 || i >= l.length || j >= l.length || j < i) { return 0; } //ok, we aren't avoid checking for i and j being zero //this code also assumes i < j if(i == (j - 1)) { return l[i]; } return l[i] + this.oneFortyNine(l, i+1, j); } I actually, don’t understand the pre-processing part of this cod...

Daily Coding Problem: Balanced parenthesis

My solution to a “Daily Coding Problem” that I received in my mail today. You’re given a string consisting solely of  ( ,  ) , and  * .  *  can represent either a  ( ,  ) , or an empty string. Determine whether the parentheses are balanced. For example,  (()*  and  (*)  are balanced.  )*(  is not balanced. Here’s my solution in Typescript, oneFortyTwo(str: string): boolean { if(str == undefined || str == null) { return false; } //not sure if this is right? if(str.length == 1){ return false; } let balanced: boolean = false; let parenStack = new Stack<string>(); for(let i=0; i < str.length; i++) { let char = str.charAt(i); let topChar = parenStack.peek(); if(topChar == null) { parenStack.push(char); } else { if(topChar == "*") { ...

Daily Coding Problem: Largest no to index i

My solution to a “Daily Coding Problem” that I received in my mail today. Given an array of numbers and an index  i , return the index of the nearest larger number of the number at index  i , where distance is measured in array indices. For example, given  [4, 1, 3, 5, 6]  and index  0 , you should return  3 . If two distances to larger numbers are the equal, then return any one of them. If the array at  i  doesn’t have a nearest larger integer, then return null. Here’s my solution in, oneFortyFour(arr: number[], i: number): number | null { if(arr == null || arr == undefined) { return null; } if(i >= arr.length) { return null; } let distance: number | null = null; let noAtIdxI = arr[i]; for(let iter=0;iter < arr.length; iter++) { if(iter == i) continue; let valAtIter = arr[iter]; if(valAtIter >= noAtIdxI) { if(distance == null) { di...

Solution: Sum Lists problem for LinkedList (in Typescript)

This post just posts the code for the solution for the sum lists problem on Linkedlist. You can see that question here, https://leetcode.com/problems/add-two-numbers/ Now onto the code class LinkedListNode { next: LinkedListNode = null; data: number; constructor(data?: number, next?: LinkedListNode) { this.data = data; this.next = next; } appendToTail(d: number) { let end = new LinkedListNode(d); let n: LinkedListNode = this; while (n.next != null) { n = n.next; } n.next = end; } length(): number { var len = 1; var current = this.next; while (current != null) { len += 1; current = current.next; } return len; } } class LinkedlistService { sumLists(l1: LinkedListNode, l2: LinkedListNode, carry: number): LinkedListNode { if (l1 == null && l2 == null && carry == 0) { return null; } ...