lek-tin:

Machine Learning Questions

What is an Eigenvalue and Eigenvector? Eigenvectors are used for understanding linear transformations. In data analysis, we usually calculate the eigenvectors for a correlation or covariance matrix. Eigenvectors are the directions along which a particular linear transformation acts by flipping, compressing or stretching. Eigenvalue can be referred to as the strength of the transformation in the direction of eigenvector or the factor by which the compression occurs. What is Gradient Descent?

by lek tin in "machine-learning" access_time 7-min read

Word Search II

Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word. Example: Input: board = [ ['o','a','a','n'], ['e','t','a','e'], ['i','h','k','r'], ['i','f','l','v'] ] words = ["oath","pea","eat","rain"] Output: ["eat","oath"] Note All inputs are consist of lowercase letters a-z.

by lek tin in "algorithm" access_time 2-min read

Coin Change 2

You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin. Example 1 Input: amount = 5, coins = [1, 2, 5] Output: 4 Explanation: there are four ways to make up the amount: 5=5 5=2+2+1 5=2+1+1+1 5=1+1+1+1+1 Example 2 Input: amount = 3, coins = [2] Output: 0 Explanation: the amount of 3 cannot be made up just with coins of 2.

by lek tin in "algorithm" access_time 1-min read

Playing with Node.js Event Loop

setImmediate/clearImmediate - will execute code at the end of the current event loop cycle process.nextTick - used to schedule a callback function to be invoked in the next iteration of the Event Loop function cb(msg){ console.log(msg); } setImmediate(cb, 'setImmediate'); process.nextTick(cb, 'process.nextTick: 1'); process.nextTick(cb, 'process.nextTick: 2'); process.nextTick(cb, 'process.nextTick: 3'); console.log('Processed in the first iteration'); process.nextTick(cb, 'process.nextTick: 4'); process.nextTick(cb, 'process.nextTick: 5'); Output: Processed in the first iteration process.nextTick: 1 process.nextTick: 2 process.nextTick: 3 process.

by lek tin in "programming" access_time 1-min read

Search a 2d Matrix II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom. Example: Consider the following matrix: [ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30] ] Given target = 5, return true.

by lek tin in "algorithm" access_time 1-min read

Max Stack

Design a max stack that supports push, pop, top, peekMax and popMax. push(x) – Push element x onto stack. pop() – Remove the element on top of the stack and return it. top() – Get the element on the top. peekMax() – Retrieve the maximum element in the stack. popMax() – Retrieve the maximum element in the stack, and remove it. If you find more than one maximum elements, only remove the top-most one.

by lek tin in "algorithm" access_time 3-min read

Subtree With Maximum Average

Given a binary tree, find the subtree with maximum average. Return the root of the subtree. It’s guaranteed that there is only one subtree with maximum average. Example Given a binary tree: 1 / \ -5 11 / \ / \ 1 2 4 -2 return the node 11. Solution public class Solution { private class ResultType { public int sum, size; public ResultType(int sum, int size) { this.sum = sum; this.

by lek tin in "algorithm" access_time 2-min read

Validate Subtree

Given two trees, T1 and T2, write an function to test whether T2 is a subtree of T1. Solution public class Subtree { public boolean isSubTree(TreeNode T1, TreeNode T2) { if (T2 == null) return true; if (T1 == null) return false; return (isSameTree(T1,T2) || isSubTree(T1.left, T2) || isSubTree(T1.right, T2)); } public boolean isSameTree(TreeNode T1, TreeNode T2) { if (T1 == null && T2 == null) return true; if (T1 == null || T2 == null) return false; if (T1.

by lek tin in "algorithm" access_time 1-min read

Minimum Path Sum in Binary Tree

Given a Binary Tree, find the minimum sum path from a leaf to root. For example, in the following tree, there are three leaf to root paths 8->-2->10, -4->-2->10 and 7->10. The sums of these three paths are 16, 4 and 17 respectively. The minimum of them is 17 and the path for minimum is -4->-2->10. 10 / \ -2 7 / \ 8 -4 Solution public class PathSum { public int Solution(TreeNode root) { if (root == null) return 0; if (root.

by lek tin in "algorithm" access_time 1-min read

Lists in Java

List interface: The List interface specifies two remove methods: List<Integer> list = new List<>(); // Remove the element at index 4 remove(int index): list.remove(4); // Remove the first occurrence of the object 4 remove(Object o): list.remove(new Integer(4)); Linked List: Pros Cons Can insert or remove anywhere in the list without shifting elements Not sortable Insert: O(1) Elements are not stored in contiguous memory addresses Remove: O(1) Find: O(n), as must traverse to find an element

by lek tin in "java" access_time 1-min read