Some useful steps to develop reusable algorithm :
Problem #1. Find maximum repeating element and count.
- Model the problem
- Find a algorithm to solve the problem.
- Analyze algorithm asking yourself, is it fast enough?, Does it fit in memory?
- If not, figure why is that?
- Find a way to address the problem.
- Iterate until satisfied.
Problem #1. Find maximum repeating element and count.
var countFreq = ary =>
{
let tempHashmap = {}, maxFreqCount = 0, maxFreqElem = 0;
for(let elem of ary){
!tempHashmap[elem]? tempHashmap[elem] = 1 : tempHashmap[elem] += 1;
if(tempHashmap[elem] > maxFreqCount){
maxFreqCount = tempHashmap[elem];
maxFreqElem = elem;
}
}
return { maxFreqCount, maxFreqElem };
};
countFreq([1, 1, 2, 1 ]) results { maxFreCount : 3, maxFreqElem = 1 }
Problem #2, common elements in sorted array
var commonElementsInSortedArray = (array1, array2) => {
let resultArray = [], pointer1 = 0, pointer2 = 0;
while(pointer1 < array1.length && pointer2 < array2.length){
if(array1[pointer1] == array2[pointer2]){
resultArray.push(array1[pointer1]);
pointer1++;
pointer2++;
}else if(array1[pointer1] > array1[pointer2]){
pointer2++;
}else{
pointer1++;
}
}
return resultArray;
};
commonElementsInSortedArray([1, 3, 5], [1, 5]); //results [1, 5]
Problem #3,
This problem was recently asked by Google.
Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
Bonus: Can you do this in one pass?
Soln : const isSumExists = (nums, sum) => {
let tempHash = {};
for(let n of nums){
let requireNum = sum - n;;
if(tempHash[requireNum] === n){
return true;
}
tempHash[n] = requireNum;
}
return false;
};
Problem 4 from leetcode [Easy].
Given an array of integers, return indices of the two numbers such that they
add up to a specific target.
You may assume that each input would have exactly one solution, and you may not
use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
Ans:
var twoSum = function(nums, target) {
let tempHash = {};
for(let [index, element ] of nums.entries()){
let neededNumber = target - element;
if(tempHash[neededNumber] > -1){
return [tempHash[neededNumber], index];
}
tempHash[element] = index;
}
return [];
};
//twoSum([3,2,4], 6);
twoSum([2, 7, 11, 15], 9);
Feedback :
Runtime: 60 ms, faster than 71.96% of JavaScript online submissions for Two Sum.
Memory Usage: 37.4 MB, less than 7.44% of JavaScript online submissions for Two Sum.
Problem #5,
Daily Coding Problem: Problem #2 [Hard].
This problem was asked by Uber. Given an array of integers, return a new array such that each element at index i of the
new array is the product of all the numbers in the original array except the one at i. For example, if our input was [1, 2, 3, 4, 5], the expected output would be
[120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6]. Follow-up: what if you can't use division?
Soln :
var elemProductAry = function(srcAry){
let productOfAll = 1;
for(const n of srcAry){
productOfAll *= n;
}
return srcAry.map(function(n){
return productOfAll / n;
});
}
elemProductAry([1, 2, 3, 4, 5]);
//elemProductAry([3, 2, 1]);
Problem #5,
Daily Coding Problem: Problem #3 [Medium].
This problem was asked by Google.
Given the root to a binary tree, implement serialize(root), which serializes the tree
into a string, and deserialize(s), which deserializes the string back into the tree.
For example, given the following Node class
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
The following test should pass:
node = Node('root', Node('left', Node('left.left')), Node('right'))
assert deserialize(serialize(node)).left.left.val == 'left.left'
Reference :
1.
https://www.dailycodingproblem.com/blog/tips-to-ace-your-coding-interview/
Comments
Post a Comment