Introduction
It is such an adventure to work on Leetcode 10: regular expression. I chose the algorithm as my most favorite mock interview algorithm on quora.com last month, the link is here. But of course I was so nervous since I could not believe what I will write down.
My practice
Here is my C# practice code. I ran the test cases, the code failed the test case with "" and pattern "a*". So I tried to fix it, added one base case, but still failed the test case. The code is here.
I told the peer that I will fix it after mock interview. I already took 43 minutes. Such a great workout.
Discussion between two peers
Here is the discussion between two peers in mock interview:
how do you match "abb" with pattern "b*b"? Peer asked, and he put down the test case next to line 68.
I think that there are 3 branches in recursion tree:
b* match 0 time, so "abb" will check to match "b", return false;
b* match 1 time, so 'a' != 'b', return false;
more than 1 time will also return fail.
Let us check "bbb" with pattern "b*b", Julia suggested to work on.
We like to check 0 time, 1 time or more than 1 time.
For 0 time case, "bbb" will try to match "b" since "b*" 0 time means empty string. Return false;
For 1 time case, b matches b*, so "bb" will try to match "b", return false;
For more than 1 time case, "bb" will match "b*b", go back to 3 cases, return true.
Feedback from the peer
I just could not believe that the peer was a competitive programmer in high school, and then he just finished his computer science graduate school two months ago. He showed to me how strong his analysis was to handle the algorithm and coding was fast and quick later on, he finished in 12 minutes and it seemed to me that he got very good training on the algorithm. Will continue on my next blog Catalan number on the performance. The peer played contest on csacademy.com.
Follow up
Nov. 8, 2017
The code is updated with code from line 37 to line 41 to fix the base case, empty string "" matchs "a*" pattern string.
C# code is here.
Two peers comparison
It is interesting to know that two peers can evaluate each other with top rating, but how about to compare the profile of two peers and see what we can tell from those numbers.
No comments:
Post a Comment