Skip to main content

Posts

Showing posts with the label typescript

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

Checking if string is numeric in Typescript and Javascript

I came across a problem of converting a char value to a number when I was working with Javascript. The interesting thing was that the solution just wasn’t as obvious as I initially thought. In this post, I will talk about how I solved the problem and why did I come across it in the first place. Background In a  previous post , I mentioned how I was solving a bunch of recursive problems on CodingBat.com! Well…I had a bit too much fun with it and I decided to extend the fun by solving  string problems  as well. Solving the string problems was good easy fun, until I came across a minor issue. Issue The problem I was trying to solve was  sumDigits . It’s a simple problem i.e. As I mentioned in my previous post and as you can see, CodingBat is focused on Java. However, the problem description is fairly generic and can be solved in any language. When writing the code for this problem the first thing that I thought of was  Math.abs . For anyone who’s wo...