Posts

Showing posts from February, 2020

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: Replace adjacent pixels in a 2d matrix (image) | Floodfill (Typescript)

My solution to a “Daily Coding Problem” that I received in my mail today. Given a 2-D matrix representing an image, a location of a pixel in the screen and a color   C , replace the color of the given pixel and all adjacent same colored pixels with   C . For example, given the following matrix, and location pixel of (2, 2), and ‘G’ for green: B B W W W W W W W B B B Becomes B B G G G G G G G B B B Here’s my solution in Typescript. I actually like this problem a lot 🙂 oneFiftyOne(matrix: string[][], i:number, j: number, target: string): string[][] { if(matrix == null) { return []; } if(i < 0 || i >= matrix.length || j <0 || j>= matrix[i].length) { return matrix; } if(matrix[i][j] == target) { matrix[i][j] = "C"; } else { return matrix; } this.oneFiftyOne(matrix, i+1, j, target); this.oneFiftyOne(matrix, i-1, j, target); this.oneFiftyOne(matrix, i, j+1, target); this.oneFiftyOn...

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

Saving user data in NoSql Firestore database

Image
Coming from a relational database background, working with a NoSql database was interesting. It requires a slightly different way of thinking about how you store your data. More specifically, I felt that was when I was building our new hybrid app with Ionic framework and Firebase/Firestore database. Some of the features of our (my) new app were user authentication and everything the user creates in the app will be saved to the user’s credentials. Sounds simple huh? Actually, if I read that, it sounds super simple to me too, however when building the app, all the tutorials I found online on Ionic, Firebase/Firestore and AngularFire, didn’t quite explain what I wanted. So, in this post, I will be showing you the logic in an Ionic/Firebase app where, the user can signup/login make (save) notes by the userid how to retrieve all the notes saved by that user In addition to the above, almost all of the Ionic tutorials I have seen online don’t provide code samples that support their ...