Wednesday, November 25, 2020

Leetcode discuss: 1368. Minimum Cost to Make at Least One Valid Path in a Grid

 Here is the link. 

Second practice - C# - no cost direction first - case study

Nov. 24, 2020
It is more efficient to handle no cost direction first using BFS, so I like to practice using C# LinkedList instead of Queue to handle the neighbor nodes.

Case study
grid = [[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]
Output: 3
How to solve the problem ?
Record minimum cost for each position in the matrix.
Using C# double linked list LinkedList, a deque data structure.

cost[0][0] = 0,
Apply BFS algorithm, start from (0,0), go right to (0, 1) or go down to (1, 0).
grid[0][0] = 1 which is to go right, so cost[0][1] = 0, no need to change the direction.
But cost[1][0] = 1 since direction should go down.
So add (0, 1) to the head of linked list, and add (1, 0) to the end of double linked list.

so the cost matrix
0, 0, ?, ?
1, ?, ?, ?
?, ?, ?, ?
?, ?, ?, ?

Next step is to visit linked list and continue to apply BFS algorithm. Work on the head of linked list first, which is (0, 1).

Check (0, 1), there are three directions, go left or right or down. Go right, no cost. Go left, cost 1, but cost(0,0) = 0, new path is bigger than 0. Ignore.
Cost matrix is the following:
0, 0, 0, ?
1, 1, ?, ?
?, ?, ?, ?

So cost matrix will be the following:
0, 0, 0, 0
1, 1, 1, 1
?, ?, ?, ?,
?, ?, ?, ?

The linked list has four nodes in the order of {[1, 0],[1, 1],[1, 2],[1, 3]}.

I think that the above steps are good enough to figure out how to find minimum cost from top left to bottom right. The next step is to work on coding. One thing is to talk about time complexity.

Double linked list
Instead of using Queue, I choose to use C# LinkedList, all neighbor nodes with no cost will be added to front of linked list, nodes with cost will be added to the end of linked list.
C# LinkedList.AddFirst for no cost direction
C# LinkedList.AddLast for cost direction

To remove a node from the linked list, remove from the head of linked list. To understand design of C# LinkedList, a C# LinkedListNode is returned from LinkedList.RemoveFirst API call.

It is important for me to remember all those LinkedListNode APIs:
Properties
List
Next
Previous
Value
ValueRef

Questions to ask
Common mistakes and questions asked:

  1. What is most important in this problem solving? All possible paths should be examined so that minimum cost will be calculated correctly from top left corner to bottom right.
  2. How to ensure that all paths will be examined? What to keep in the search?
  3. Why do min cost of each position (row, col) in matrix should be calculated in order to get the bottom-right one?
  4. Why does the solution using two loops, one is to iterate each row, one is to iterate each column, it is not working? Is it a common mistake from interviewees?
public class Solution {
     /// <summary>
        /// Nov. 24, 2020
        /// BFS approach 
        /// Work on BFS approach - work on no cost direction first, and then directions with cost next
        /// Using C# LinkedList instead of Queue, deque - two ends. 
        /// </summary>
        /// <param name="grid"></param>
        /// <returns></returns>
        public int MinCost(int[][] grid)
        {
            var rows = grid.Length;
            var columns = grid[0].Length;

            var cost = new int[rows][];
            for (int row = 0; row < rows; row++)
            {
                cost[row] = new int[columns];
            }

            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < columns; col++)
                {
                    cost[row][col] = int.MaxValue;
                }
            }

            cost[0][0] = 0;

            var list = new LinkedList<int[]>();
            list.AddLast(new int[] { 0, 0 });

            //                              right,    left,      down,      up 
            var directions = new int[,] { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
            //  1 -> right, 2 <- left, 3 down, 4 up 

            while (list.Count > 0)
            {
                var node = list.First;
                list.RemoveFirst();

                // C# LinkedListNode - Value - get, set, Next, Previous
                // https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.linkedlistnode-1.value?view=net-5.0
                var row = node.Value[0];
                var col = node.Value[1];

                int currentCost = cost[row][col];

                for (int i = 0; i < directions.GetLength(0); i++)
                {
                    var nextRow = row + directions[i, 0];
                    var nextCol = col + directions[i, 1];

                    // grid[x][y] has 4 values 
                    // 1 -> right, 2 <- left, 3 down, 4 up 
                    var noAdditionalCost = grid[row][col] == (i + 1);
                    int newCost = currentCost + (noAdditionalCost ? 0 : 1);

                    if (nextRow >= 0 && nextCol >= 0 && nextRow < rows && nextCol < columns)
                    {
                        // current path is shorter one
                        if (newCost < cost[nextRow][nextCol])
                        {
                            cost[nextRow][nextCol] = newCost;
                            var nextPosition = new int[] { nextRow, nextCol };

                            if (noAdditionalCost)
                            {
                                list.AddFirst(nextPosition);
                            }
                            else
                            {
                                list.AddLast(nextPosition); 
                            }
                        }
                    }
                }
            }

            return cost[rows - 1][columns - 1];
        }
}


Equity research: I am back and I like to work hard

 Go over finviz.com and start to learn more about business. 

 Today I went over a few stocks, and I am looking for good stocks to invest in short term or long term. 

 I like to be open and be a hard working good researcher. 



Tuesday, November 24, 2020

NYSE EXPR: 300% gains from Nov. 6, 0.62 - Nov. 24, 1.54

 Nov. 24, 2020

Introduction

It is important for me to stay in current. Look for best opportunity in stock market. Take some risk. 

Let SU.TO stock and Canadian oil stocks be the history. Embrace new challenge with those distress retail clothing store. 

I like to be an investor and then try to stay in long term as well. 

EXPR - Express, Inc. 

My favorite brand and I wish that small business can survive. 

Thanks Anna to be a good researcher. She is such hard working lady and gets up early at 5:00 AM every day. 

Equity research is most important for us. There is a fair opportunity for all of us to build wealth in this coronavirus special time, once a century opportunity. 




Sunday, November 22, 2020

Lalit Kundu: #CodingInterviewWeekly series: Exploring a dynamic programming and graphs Google interview

 Here is the link. 

You are in a matrix where each cell has some coins placed on it. You can move in either of the 4 directions (up, left, right, bottom) where the movement is possible only if the cell you want to move to has strictly greater number of coins than your current cell. You can start from any cell. What is the maximum length of the path you can move? 

Let's also try a follow-up: Find a cell from which maximum number of cells are reachable using the rules we've mentioned. 

Leetcode 329. Longest Increasing Path in a Matrix, hard level algorithm. My last practice is on May 23, 2019.


I am preparing google onsite on Dec. 8, 2020. I watched the video again and agin, and after I spent time to go over Leetcode 329 Longest increasing path in a matrix (hard level), my last practice in 2019 and all other sharings, I started to understand your teaching much better. You covers so many topics: dp states, infinity loop, DAG, recursive function, best algorithm lesson in the world. I am really curious and like to learn more about hire and strong hire in last five minutes.

Hide reply
Probing with Lalit Kundu
Glad you find it useful - you're right, I have a habit of digressing into literally everything that's related to the crux of the problem :) In future videos, I will surely talk about various aspects of the interviews - such as interviewer ratings, implementation tips etc. - stuff that I haven't covered in this video.



Jeff H Sipe: Open-Ended Interview Questions - How To Master Questions With No Structure

 Here is the link. 

0:54 - The Why 1:33 - Breathe 1:51 - Use the Whiteboard 2:14 - Reiterate the Question 2:44 - Follow Up/Clarifying Questions 3:07 - Solve - Including Example Answer Other videos referenced as cards in this video: 0:19 - Vague Questions - https://www.youtube.com/watch?v=BxKRO... 0:54 - Screen Share - https://www.youtube.com/watch?v=VCx4l... 2:16 - Restate the Question - https://www.youtube.com/watch?v=uFuoa... 2:56 - Tough Interview Questions - https://www.youtube.com/watch?v=xcpvr... 3:07 - Keep it Relevant - https://www.youtube.com/watch?v=uFuoa... Additional Videos that might help: Google Interview - https://www.youtube.com/watch?v=ZD3NX... Program Manager Interview - https://www.youtube.com/watch?v=Uk70u... Program Manager Interview #2 - https://www.youtube.com/watch?v=7EEsr... Questions to Ask Your Interviewer - https://www.youtube.com/watch?v=5sjcu... The Job Description - https://www.youtube.com/watch?v=WJTOr... Types of Questions - https://www.youtube.com/watch?v=vFdkh... Interview Prep Checklist - https://www.youtube.com/watch?v=XjEg9... The STARL Method - https://www.youtube.com/watch?v=CVeFe... How to End Your Answer - https://www.youtube.com/watch?v=rAzFy... Follow Up/Clarifying Questions - https://www.youtube.com/watch?v=n4T1H... Add a Little Detail to Your Answers - https://www.youtube.com/watch?v=AvWMV... How to Take Control - https://www.youtube.com/watch?v=JUQcg... The Job Description - https://www.youtube.com/watch?v=WJTOr... Types of Questions - https://www.youtube.com/watch?v=vFdkh... Social Media: Connect with me on LinkedIn - https://www.linkedin.com/in/jeffhsipe/

Jeff H Sipe: The STAR Method - RESULTS

 Here is the link. 

1:20 - Great Results 1:40 - Alignment 2:15 - Taking Credit 2:52 - Be Specific 3:31 - Multiple Positive Results 4:04 - Focus Areas 6:48 - End of Story 7:24 - Not Completed 7:56 - Learnings 8:30 - Results First ▶️ Behavioral Questions Doc: https://docs.google.com/document/d/1n... Other videos referenced as cards in this video: 0:12 - Behavioral Questions - https://www.youtube.com/watch?v=sq3py... 2:52 - Details - https://www.youtube.com/watch?v=AvWMV... 3:31 - Always be Positive - https://www.youtube.com/watch?v=SQt61... 7:56 - STAR + Learnings - https://www.youtube.com/watch?v=CVeFe... Additional Videos that might help: STAR - Situation - https://www.youtube.com/watch?v=DrJP2... STAR - Actions - https://www.youtube.com/watch?v=ukJM8... STARL + F - https://www.youtube.com/watch?v=T2TwG...


Onecareer: Behavior question: Do you have any questions

 Here is the article. 

这也是一个展示自己的绝佳机会,用提问题突显你的专业或态度,让你有加分的可能。

 

除此之外,还有以下目的:

 

  • 考察你对公司基本信息的了解和熟悉程度。

  • 提出好问题的能力甚至比就问题给出好的答案更重要,因此单面中设置提问也是考察同学“提出问题”的能力。

  • 提问的角度和内容能反映你对公司和岗位的兴趣和热爱程度

  • 提问也是你对企业做出评判和选择的机会,毕竟面试是互相考验的过程,你也需要评估公司和岗位的发展前景,以及来到这家公司工作对你的职业生涯有没有帮助。


所以,这个问题非常关键。如果问对问题,除了可以让自己的专业性提升外,还可能更了解公司内部,甚至翻转面试官对你的整体印象。

Wechat: Onecareer - Plan to spend 60 minutes to read

 Nov. 22, 2020

Introduction

I like to take some time to read wechat - onecareer. Behavior interview is very challenge for me. I do not know what is best way for me to reach offer stage. I am still working on algorithm and data structure practice, and I like to take time to learn how to behave better. 

Wechat - Onecareer16

I like to read behavior interviews in Chinese. 



Behavior interview: How to answer the question in Chinese?

 Here is the link. 




Leetcode premium community: I should join Leetcode premium early

 Nov. 22, 2020

Introduction

It is tough 2020. I spent over 6 months to work on trading, and my coding skills falls behind. I chose to paid leetcode premium since I like to prepare Google onsite, and then I learn more from Leetcode premium community. 

Premium community

I have to work on 14 set Google onsite mock interviews. It is tough for me to speed up and I could not solve three or four algorithms in 45 minutes.