Showing posts with label mock interview successful training. Show all posts
Showing posts with label mock interview successful training. Show all posts

Saturday, December 2, 2017

code review: Find the smallest substring that contains some given subset of characters

Dec. 2, 2017

Introduction


The sliding window algorithm is very challenge one even after I posted Find the smallest substring that contains some given subset of characters  two months ago. Today 12 PM I had a mock interview and then I have to work on the similar algorithm with a peer from the United Kingdom. With the peer's help, I spent 50 minutes to go over the analysis and coding but still could not finish the algorithm writing, we had a chat about how to work on algorithm after we worked on both interviews 80 minutes. The peer asked a break, we took 20 minutes break to chat about algorithms. In last 5 minutes, I was asked by peer to finish the algorithm. I almost finished but timeout, time limit is 110 minutes. The fact is that I lost my writing on the platform.

I need to recall what I did about the coding, and then post the algorithm here. The peer had good advice for me to write the algorithm this time, the code will be different from the one I wrote before.

Coding


Now it is Dec. 3, 12:09 AM. This version of code passes 2 test cases, fail 5 test cases. The C# code is here.

There are two issues in the code, first the input ["A"], search string "B", the result should be empty, not "B". Second issue is "Index was outside the bound of the array".

Continue to work on the bug fixing.

Now it is 12:35 AM. I fixed all the bugs. The function's arguments are not meaningful, so I mixed them together, one is arr, one is str. There are more than four places I mixed them in C# code here, line 26, arr[index] should be str[index], same as line 45, line 47. I changed them to meaningful name using char[] source, string search.

Lesson learned


Lesson learned: Always use meaningful name. Express the intent. I should change the variable's name to avoid the errors at the very beginning.

The C# code is here to look up.
Line 58
var isNeeded = dictionary[visit] > 0;

Actually I wrote first like this:
var isNeeded = Array.IndexOf(arr, visit);

And the peer asked me the time complexity of Array.IndexOf, it is O(m) and m is the length of the arr length. I should make it O(1) time instead. The peer worked very hard to help me, he followed my analysis of algorithm, time complexity O(n) where n is the length of search string instead of O(mn).

And the bugs are fixed to make the changes on the following lines.

Line 65 - add dictionary.ContainsKey(current) to avoid run time exception. The peer told me to move line 77 - 85 inside the while loop starting from 58 to 88, to make the code more simple to write.

ToDictionary using LINQ


Julia wrote the code in mock interview, she is still learning how to write LINQ statement. She memorized the tip she got from the code review.

var dictionary = arr.ToDictionary<char, int>();

And the peer was confused, and asked what I was writing. So, I wrote a for statement instead right away. I did not know that I have to write two mapping for key and value using LINQ statement in mock interview.

The LINQ learning is challenging, I should write
var dictionary = arr.ToDictionary(c => c, c => 1);


Discussion between two peers


It is the first time Julia learned that how good a peer can perform on the recursive algorithm, specially how to compose the test case quickly with the art of simplicity, and followed with the code.

Julia also learned that a programmer from other side of earth, North Ireland. Julia thought that the peer must work for Nokia before, but it ended up that Finland is far away from North Ireland. The peer corrected Julia on this.

The peer praised that Julia did very well on analysis of the algorithm, best one in last 5 or 6 peers worked on the algorithm. Julia gave honest response. It is all about hard work. Here is the code review about the algorithm on stackexchange.com, Julia worked on the algorithm over 2 weeks in 2015, and then she practiced mock interview on the algorithm over and over again. Here is the blog about over 5 or 6 mock interviews on the algorithm wrote in April 9, 2017.

Julia later looked up the university of North Ireland, Queen's University Belfast and talked to her roommate about Great Britain, compared to University of Victoria.

Tuesday, June 9, 2015

LeetCode article: Finding the Minimum Window in S which Contains All Elements from T

April 26, 2015
Problem statement:
Finding the Minimum Window in S which Contains All Elements from T

Introduction


I tried to solve one algorithm in 5 days in a row from January 28 to February 3 in 2015, every two hours after 6 PM in a week. But I could not do it successfully. 

The algorithm is called find smallest substring containing unique keys. At that time, I did not have the peer to talk to, and I came home after the full time work, I wrote the code on the paper, 2 hours each time from Monday to Friday. 

I wrote on papers, page by page. I tried so many times but there is always new issues coming out. It is the first time I learned that there is brute force solution, and time complexity can be analyzed using kind of mathematics. 

The idea to solve the solution is to use slide window and also keep the minimum time complexity by tracking the unique keys inside the slide window by maintaining a variable.
从2015年1月28到2月3日, 每天二小时, 总共14个小时, 我练习写这个算法。

最优解是用O(N)的时间, N是字符串的长度。

从一个简单的例子, 开始分析。 
比如, abc 是要寻找的字符, 另外一个字符是 abbca; 如何从abbc, 找到 bca?

最笨的方法, 就是起点有多少选项, 终点有多少选项, 时间复杂度就是O(N2)。
最笨的方法就是O(N x N), 想要最优, 只能走一遍, 二个指针, 起点, 终点, 如何移动, 保证起点移动是在O(N)的算法中, 终点的也在. 设计中小心不要移太多.

Follow up after 10 days

on June 5, 2015
Algorithm blog in Chinese, with good example and illustration of the algorithm. Blog link is here.

Follow up after 24 months 

April 9, 2017

Follow up Feb 5 2018

One thing I like to do is to read the above blog in Chinese, and then make notes more readable, and then go over the source code as well. Save notes and code as two gists, and later I will write a C# solution based on the source code.

Write down the notes in the above blog and pay attention to the thinking process. Here is the gist.