Thursday, July 4, 2019

310 13316 Old Yale Rd 310, Surrey, BC V3T 5J5

Here is the link. 

$269,785

Totally renovated and move in ready this sparkling clean one bed condo at the quiet sunny back of the building is ready for you to call home. No need for a car just walk to shops, services, transit and all the action Surrey Central has to offer SFU, Holland Park, 2 skytrain stations, T&T and more. Underground and visitor parking accessed at the back. Low monthly mtnce fee includes heat and hot water. Large balcony overlooking gardens and club house on quiet sunny side of building also accesses private storage locker. Take out laundry pair and use shared laundry and now you have a second bedroom or den. Freshly painted and new floors throughout, kitchen had new counter tops dishwasher and stove. 2 pets allowed cat or dog. no rentals, no age restriction.

  • Price:
  •  
  • $269,785
  • Bedrooms:
  •  
  • 1
  • Full Baths:
  •  
  • 1
  • Square Footage:
  •  
  • 714
  • Year Built:
  •  
  • 1981
  • Listing ID #:
  •  
  • R2384723
  • Street Address:
  •  
  • 310 13316 Old Yale Rd 310
  • City:
  •  
  • Surrey
  • Province:
  •  
  • British Columbia
  • Postal Code:
  •  
  • V3T 5J5
  • Community:
  •  
  • North Surrey
  • Subarea:
  •  
  • Whalley
  • Listing Status:
  •  
  • Active
  • Age Of Dwelling:
  •  
  • 38
  • Amenities:
  •  
  • Club House, Elevator, In Suite Laundry, Shared Laundry, Storage, Wheelchair Access
  • Features:
  •  
  • Clthwsh/Dryr/Frdg/Stve/Dw, Drapes/Window Coverings
  • Miscellaneous Features:
  •  
  • Central Location, Lane Access
  • Stories:
  •  
  • 1
  • Style:
  •  
  • Rancher/Bungalow
  • Total Building Square Footage:
  •  
  • 714
  • Year Built:
  •  
  • 1981
  • Title Type:
  •  
  • Freehold Strata
  • Association Fee:
  •  
  • 290.97
  • Property Taxes:
  •  
  • $787
  • Tax Year:
  •  
  • 2017

Wednesday, July 3, 2019

Amazon phone screen - find shortest distance between two nodes in binary tree

The discussion link on Leetcode is here. 


May 18, 2017 2:07 PM
I thought about the algorithm and it should fit in 60 minutes time range, and also need to make the code work, pass a few test cases.
First, write recursive function instead of iterative. Code is simple.
Secondly, recursive is a depth first search algorithm, treat it as a graph search algorithm.
Third, I like to argue that "find lowest common ancestor" maybe is a more complicated algorithm. Better not relate to the algorithm "find lowest common ancestor".
To find a path from root node to search node, the function is designed to find one node a time. The time complexity should be the same to find two nodes one time.
Time complexity is O(n), n is the total nodes of binary tree. Use preorder traversal, visit root first, then visit left and right child.
Here is my C# practice code.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BinarySearchTreeTwoNodesDistance
{
    class Program
    {
        internal class Node
        {
            public int Value { get; set; }
            public Node Left { get; set; }
            public Node Right { get; set; }

            public Node(int number)
            {
                Value = number;
            }
        }

        static void Main(string[] args)
        {
            // calculate two nodes distance
            RunTestcase();
        }

        /// <summary>
        /// inorder traversal - 1 2 3 4 5 6 7
        /// </summary>
        public static void RunTestcase()
        {
            var root = new Node(4);
            root.Left = new Node(2);
            root.Left.Left = new Node(1);
            root.Left.Right = new Node(3);
            root.Right = new Node(6);
            root.Right.Left = new Node(5);
            root.Right.Right = new Node(7);

            // distance should be 4 
            var distance = FindDistance(root, root.Left.Right, root.Right.Right);
            Debug.Assert(distance == 4);

            var distance2 = FindDistance(root, root.Left.Right, root.Left.Left);
            Debug.Assert(distance2 == 2); 
        }

        public static int FindDistance(Node root, Node p, Node q)
        {
            IList<Node> possiblePath_1 = new List<Node>();
            IList<Node> possiblePath_2 = new List<Node>();

            IList<Node> searchPath_1 = new List<Node>();
            IList<Node> searchPath_2 = new List<Node>();

            FindPath(root, p, possiblePath_1,ref searchPath_1);
            FindPath(root, q, possiblePath_2,ref searchPath_2);

            if (searchPath_1.Count == 0 || searchPath_2.Count == 0)
            {
                return 0; 
            }

            // find first node not equal 
            int index = 0;
            int length1 = searchPath_1.Count;
            int length2 = searchPath_2.Count;

            while (index < Math.Min(length1, length2) &&
                searchPath_1[index] == searchPath_2[index])
            {
                index++;
            }

            //(length1 - 1) + (length2 - 1) - (2 * (index - 1))
            return length1 + length2 - 2 * index;
        }

        /// <summary>
        /// Do a preorder search for the node
        /// </summary>
        /// <param name="root"></param>
        /// <param name="search"></param>
        /// <param name="possiblePath"></param>
        /// <param name="searchPath"></param>
        public static void FindPath(Node root, Node search, IList<Node> possiblePath, ref IList<Node> searchPath)
        {
            if (root == null || searchPath.Count > 0)
            {
                return;
            }

            if (root == search)
            {
                searchPath = possiblePath;
                searchPath.Add(search);
                return;
            }

            possiblePath.Add(root);
            IList<Node> leftBranch  = new List<Node>(possiblePath);
            IList<Node> rightBranch = new List<Node>(possiblePath);

            FindPath(root.Left, search, leftBranch, ref searchPath);
            FindPath(root.Right, search, rightBranch,ref  searchPath);
        }
    }
}

Actionable Items


July 3, 2019
I should be able to write a few lines of code to solve the problem. I just could not believe that my post has so many issues. It is too time-consuming, and there is elegant solution out there.

There is more simple idea. Use post order to find all nodes on the path from given node p to root node, and save it into hashmap, key is treeNode, value is the distance to given node p; second call using given node q, and then do the same work as given node p, but this time, check all nodes from node q to root to see if it is in hashmap or not, if it is, then lowest common ancestor is found. Add current distance to distance from hashmap. Time complexity: O(N)

KMP algorithm review

I like to find some best study notes for KMP algorithm. Here is the post written by Facebook engineer manager in London. Not a bad idea to learn more about an outstanding engineer in the same time.

28. Implement strStr()

Here is my discussion post I shared today.


57. Insert Interval

I like to add two posts to share my practice today, now it is 10;25 PM, I just spent 10 minutes to write down my review quickly.

Here is the first one.
C# Sort intervals using start time first practice in 2017 is the second one.

Actionable Item


I think that it is a good strategy to share my practice first, and then I will come back to review and make my practice better.


394. Decode String

I like to study the algorithm if I have 10 minutes to work on.

1036. Escape a Large Maze

It is a hard level algorithm, and I saw it in Google phone screen post. I like to spend 10 to 20 minutes to study it in short future.


212. Word Search II

I was shy and chose not to share my C# practice on Leetcode.com from 2017 January to Jaunary 2018. Today I shared all my three submissions on Leetcode.com.

Here are the links:

1. First practice is here.
2. Second practice is C# practice using Trie and good explanation in detail.
3. Third practice is C# Using Trie with a case study practice in January 2018.

Fear and greed in algorithm problem solving

July 3, 2019

Introduction


It is my personal finance research. I learn that fear and greed is the biggest problem for an investor. I also like to look into this issue when I work on algorithm problem solving. Emotion is a biggest enemy in investment, can I say same thing in algorithm problem solving?

Fear

I worry about so many things, first five year full time from 2010 to 2015, I did not work on algorithm problem solving at all.

Compare to the time range from 2010 to 2015, I believe that I learn much better from 2015 to 2019. Since I started to work on coding blog, code review stackexchange, mock interview, Leetcode, I push myself to learn and meet so many people.


Greed

I was very greedy. From 2010 to 2015, I worked full time but I did not know how to advance myself in terms of career. My naive thinking is not to take any sick day for first two years, no vacation day for the first whole year. I was so greedy.

But it turned out that I did not balance very well. I did not check my Par 401 K statement from 2007 to 2019 monthly; I did not learn how to put my IRA CD into stock market, I did not learn how to invest my retirement fund.





97. Interleaving String

It is a hard level algorithm. I think that it is better for me to spend some time to write a post and share my learning from the algorithm as well. I am planning to write a solution again in next one or two days, just make sure that I can handle edge case properly, and I can handle it in less than 10 to 15 minutes.

Here is my post.

It is challenging to come out dynamic programming solution. One idea is to write down some good thoughts and explain why this one is different from other dynamic programming solution.
Do not memorize the solution
I like to ask the question how to define interleave, why there is no small example in problem statement. I reviewed my own code and was surprised that I did not write down any thought related to interleave. I have to push myself clarify the question first.
Ask clarification questions
I think that we should have a small example to explain how to interleave is accurately defined in problem statement.
s1 = "aa"", s2 ="bb", how to interleave those two string s1 and s2?
The interleave string should keep chars in s1 in original order, chars in s2 in original order. For example, first char can be selected from s1 or s2, both start from index = 0, so it can be 'a' or 'b', and then continue to second char, so it can be two choices from s1 or s2, but it should be first char not visited yet in s1 or s2.
The problem space should be O(N * M), N is s1's length, M is S2's length, so we can build transition state table to solve it from bottom up. If it is not interleaved, then problem space may go up to 2^(N + M) level, it can not be solved using dynamic programming.
Play first and then write the code
Given s1 = "dbbca", s2 = "aabcc", s3 = "aadbbcbcac", how to solve it using transition table in the following :
image
image
subproblems to work on:
s3 = "aa", take the first char from s2, and next char from s2, so it should be true for "aa" and "".
s3 = "aad", take the first two chars from s2, and then first char from s1, so it should be true for "aa" and "d".
I found the second path to the interleave string S3.
image
Case study: "aa" and ""bb", interleave "abab"
It is better to work on a small test case, and explain how to approach this problem in general. For example, S1 = "aa", S2 = "bb", then interleave string can have at least 2 * 2 cases by just working on first two chars, "aabb", "abab","baba","bbaa". In total, there are six interleave strings, two more, "abba", "baab".
Work on the matrix, explain how to build a bottom up solution starting from base cases.
image
Warm up combinatorics
image
If first 'a' take index = 0, then second 'a' has three choices from index = 1 to 3;
If first 'a' take index = 1, then second 'a' has two choices from index = 2 to 3;
If first 'a' take index = 2, then second 'a' has one choice taking index = 3.
So total choice is 6. The empty two space for "bb", it has to maintain the original order.
This simple example still can be managable. But what if two strings are longer, the brute force solution can be messy. That is the motivation for me to learn dynamic programming and solve the problem without too much detail.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _97_Interleave_strings
{
    class Program
    {
        static void Main(string[] args)
        {
        }

        /// <summary>
        /// 97 Interleave strings 
        /// Dynamic programming solution
        /// </summary>
        /// <param name="s1"></param>
        /// <param name="s2"></param>
        /// <param name="s3"></param>
        /// <returns></returns>
        public bool IsInterleave(string s1, string s2, string s3)
        {
            if (s1 == null || s2 == null || s3 == null)
                return false;

            var length1 = s1.Length;
            var length2 = s2.Length;
            var length3 = s3.Length;

            if (length3 != length1 + length2)
                return false;

            if (length1 == 0)
                return s2.CompareTo(s3) == 0;

            if (length2 == 0)
                return s1.CompareTo(s3) == 0;

            var rows = length1 + 1;
            var columns = length2 + 1;
            var dp = new bool[rows, columns];

            dp[0, 0] = true;
            // base case: first row
            for (int col = 1; col < columns; col++)
            {
                dp[0, col] = s2[col - 1] == s3[col - 1] && dp[0, col - 1];
            }

            // base case: first column
            for (int row = 1; row < rows; row++)
            {
                dp[row, 0] = s1[row - 1] == s3[row - 1] && dp[row - 1, 0];
            }

            // bottom up - check left and top 
            for (int row = 1; row < rows; row++)
            {
                for (int col = 1; col < columns; col++)
                {
                    var visit1 = s1[row - 1];
                    var visit2 = s2[col - 1];
                    var visit3 = s3[row + col - 1];  // bug: my first writing is row + col - 2

                    // check subproblems - two subproblems, left and top two cases. 
                    dp[row, col] = (visit3 == visit1 && (row - 1 >= 0 && dp[row - 1, col])) ||
                                   (visit3 == visit2 && (col - 1 >= 0 && dp[row, col - 1]));
                }
            }

            return dp[rows - 1, columns - 1];
        }
    }
}


88. Merge Sorted Array

Here is the link.

Since it is an easy level algorithm, I just took the straightforward the solution to merge two sorted arrays.
Here are highlights:
  1. Calculate the merged array's length;
  2. Copy backward to the merged array, from each end of the two arrays to start to merge.
public class Solution {
    public void Merge(int[] nums1, int m, int[] nums2, int n)
        {
            if (nums1 == null || nums2 == null)
                return;

            var length = m + n;

            var end1 = m - 1;
            var end2 = n - 1;

            var index = length - 1;
            while(index >= 0)
            {
                var moveFirst = false;
                if (end1 < 0)
                {
                    moveFirst = false;
                }
                else if (end2 < 0)
                {
                    moveFirst = true;
                }
                else
                {
                    var current1 = nums1[end1];
                    var current2 = nums2[end2];
                    moveFirst = current1 > current2;
                }

                if (moveFirst)
                {
                    nums1[index] = nums1[end1];
                    end1--;
                }
                else
                {
                    nums1[index] = nums2[end2];
                    end2--;
                }

                index--;
            }
    }
}


Algorithm review

July 3, 2019
It is important for me to stay organized, and also I need to push myself to write down my thinking process to practice those algorithms. Here are some algorithms I reviewed recently. 
  1. Word Ladder
  1. Next Greater Element III
    C# using stack practice in August 2018

Tuesday, July 2, 2019

Charlie Munger's advice on investing and life choices that make a person wealthy

Here is the link.


13696 100 Ave Surrey BC - Asking price $288,000

Here is the link.
BCCondos webpage is here.
Here is page to show Chinese content.


2615 13696 100 Avenue $288,000 asking price

I try to understand why the unit is below the market price.

物业详情


  • 公寓
  • 洗衣机/干衣机/冰箱/灶具/洗碗机
  • 会所,健身中心,花园,温室,康乐中心,温水泳池
  • 222.38加币/每月
地点!毗邻轻轨,沃尔玛购物中心,T&T超市,由Concord太平洋建造,低层费,许多设施,1间卧室,1间浴室,1个停车场和1个储物柜。面向东方,美景如同我们在26楼,510平方英尺的矩形地板布局,燃气灶,与Metrotown Met1 Design几乎相同。楼下的大泳池,热水浴缸等,步行距离的SFU,很适合从这里开始学习和工作,周围有你需要的所有,抵押贷款压力较小。拥有时间灵活,** 7月6日和7日的第一个开放式房间坐下来,下午2点到下午4点 

Monday, July 1, 2019

Write high performance .NET core code

Here is the link.


Next bear market study

Here is the link.

经济衰退和股市估值泡沫

10个潜在下行风险
新兴市场债务危机、全球贸易局势

经济衰退

持续倒挂的美债收益率
纽约联储衰退概率模型、领先经济指标、芝加哥联储国家经济活动指数、密歇根大学消费者信心指数和美国失业率等一系列数据和指标中

纽约联储衰退概率模型
领先经济指标
芝加哥联储国家经济活动指数
密歇根大学消费者信心指数
美国失业率等一系列数据和指标中

欧元区6月ZEW经济景气指数、德国6月ZEW经济景气指数

基金经理在5月份的股票配置已下跌32个百分点至21%,相对应地,债券配置规模上升至2011年9月以来的最高点

6月份接受调查的230名基金经理中
做多美国国债首次成为华尔街最热门交易

描述对冲基金对股票信心的标普500贝塔指数正在测试2003年以来的低点——2.3%

依靠高企的债务进行支撑。科隆博在其报告中详细剖析了美股估值的严重膨胀程度,得益于美联储的刺激性政策,美国股市自2009年的低点已经上涨了300%。

KEJ  Financial Advisors主席Jonathan  Heller

数据显示,第二季度迄今罗素2000指数下跌约1%,罗素微型股指数下跌约2.6%,股市上出现明显的向优质资产转移现象。这意味着投资者对股市等风险资产的信任度正在降低,避险情绪仍主导市场行为。

短期技术指标显示,市场经历了从极度超卖状态到极度超买的剧烈逆转,目前的价格比200日均线高出6%以上。从短线投资的角度看,分析师认为投资者最好等待一个修正过程,至少等美股回到200日均线水平,然后再承担额外的股票风险。

保证金债务正以每年15%的速度下降,从历史经验来看,这一低到令人发指的水平,意味着标普500指数将出现最高达20%的暴跌。

本轮史上最长牛市的头号功臣——企业回购

用来衡量标准普尔500指数下行风险的活动指标(CAI)出现异动,股市再次出现衰退10%的预警信号

恐慌指数VIX近期走势可能会给投资者带来一些误导,需要仔细分析才能看出个中端倪

企业回购,尤其是减税法案生效后美国科技股企业的大规模回购行动,是本轮美股牛市的强大支撑。

数据显示,美国排名前10位的科技公司(Alphabet、亚马逊、苹果、思科、Facebook、英特尔、国际商业机器公司、微软、甲骨文和高通公司)在2018年花费超过1690亿美元回购其股票,较减税前一年激增55%,创下历史新高。通过创历史的大规模回购,股票价值和企业盈利都得到支撑,在全球经济逐渐见顶的情况下,守住了美股最后的繁荣。

从数据来看,标普500强企业目前的预期市盈率达到了17倍左右,高于15至16倍的历史平均水平。

Actionable Items


I like to look into the following items. 


A friend makes the world connected

July 1, 2019

Introduction


It is a long story. I met this young undergraduate student on pramp.com in Apri 1, 2018. One week ago, he messaged me if I am still working on algorithm practice, he asked me if I can help him to prepare Facebook onsite in September, 2019. I said yes.

First mock interview


Since I am preparing the first technical phone screen in 2019, the young talent offered me a mock interview. He is the interviewer.

I opened a github repository for our meetup. Here is the repository.

Actionable Items


I need to get more mock interview on pramp.com in next week. I need to learn how to perform under stress as an interviewee.

Here are lessons I learned through the mock interview.

1. I should ask the intervewer to clarify the requirement first, space is O(1).
2. I should work on time complexity and what I can achieve in my design.
3. The interviewer spent 50 minutes to discuss with me. He gave out hint too quickly, he dropped hints more than twice.
4. He asked me what edge cases I can come out.
5. He gave me test cases to work on:

1 + 2  + 3
1 + 2 * 3
1 + 2 * 3 * 4
1 + 2 * 3 * 4 * 5
1 + 2 * 3 * 4 * 5 + 6

-------------------------->

To analyze the test case with more than one multiplication, use while loop.
    2 * 3 * 4 * 5
    -------------> while loop

I should have a timer on my screen, so I can calm down and also plan very well what to say, how to approach the problem in most efficient way. I found out by looking at timer once a while in mock interview, I learn how to approach the problem in most efficient way, since I try to avoid complicate solution, compete with most optimal solution.

The 10 Worst Mistakes of Beginning Traders

Here is the link.