Wednesday, April 27, 2022

Leetcode submissions: My history from 2015 to 2022 | Ideas to make more submissions

April 27, 2022

Introduction

I started to work on Leetcode practice after I had one hour onsite with Facebook in the city of Vancouver. I still remembered the day and I was so excited but I could not perform very well. It is a simple algorithm using sliding window. I decided to work on Leetcode, and started to practice. It is a long journey, and then I got 4 Amazon onsite, three Facebook onsite, one Google onsite, and two Microsoft onsite. I am preparing Meta onsite and Google phone screen. 

Submission history | 2015 to 2022












Ideas to make more submissions

I definitely think that it is important to learn from Leetcode discuss post. I should try to learn different languages, for example, read some Python code, and make submissions, it is fast and easy way to learn Python. 

Try different ideas to work on algorithms, try to work on easy level algorithms first. And also it is fun to learn a few more solutions for each algorithm I practice and try to write my own code using C#. 

Definitely it is easy to tell that I am not good to manage submissions. It is definitely a good training tool to practice on leetcode.com. I took too long breaks in-between and I do not have consistent history of submission. My peak submission count is not competitive compared to other top-ranking players. 

It is easy now to follow top players most voted discuss post. I can choose 10 favorite players, and review 10 algorithms from each player top voted discuss post. 

Follow up

I put together a picture with all last 7 years submission history, so I can figure out what is my favorite month to practice and why it is my favorite month. 



I should have a check list for Leetcode algorithm practice:

  1. Make a three months plan, one month plan, or one week plan for practice;
  2. I used to take weekly contest in the weekend, so that at least I will practice four algorithms a week;
  3. It is better to work on most popular or most voted algorithms; 
  4. I paid for Leetcode premium membership, 2022 is the second year to pay premium fee. 
  5. Have a monthly, quarter, or annual review on Leetcode practice. 
  6. Think about better ways to practice besides Leetcode. 
  7. Work on a few more ideas later. 

Udemy | The "BigTech" System design interview Bootcamp

April 27, 2022

Introduction

It is always a good strategy to take some notes, and then I will focus more on my study. It is very good course and I like to learn better, and also follow up with more research. 

Authentication

  • Password-based
  • Token-based
  • Multi-factor
  • Certificate-based
  • Single sign-on
Section 3: Netflix: Capacity estimation

1. Estimate - request per second
  • How many active users Netflix has?
  • How many requests/min are required to stream a video?
  • The daily watch time of a user
    720 req/sec
2. Estimate - storage
    
  • The amount of available videos
  • The size of each video
    30 TB 
3. Estimate - Bandwidth
    25 GB/ sec

Section 4: Netflix Data Model

    15. Create data model
    
    16. Suitable databases

Key-value store
videoID: title, summary, video_url, thumbnail_url, length, rating 

Database selection

key-value store  - key-value store for metadata
Data store - Data store for video and thumbnail
Search engine - search DB for subset of video metadata

Search engine DB
  • Store subset of metadata
  • NoSQL DB
  • Used indexes to categorize
Search engine

Relational database - relational DB for users and subscriptions

Section 5: Netflix streaming feature 
    




Requirements check-in

video streaming
search
user accounts
subscription

Availability
Scalability
Reliability
Security 




Tuesday, April 26, 2022

Monday, April 25, 2022

Elon Musk | Twitter | Business analysis of Twitter

 

马斯克成功收购推特后,将做出哪些改革?


周一,社交媒体公司推特(Twitter)接受了马斯克提出的以440亿美元收购该公司的提议,如果交易成功,将标志着科技史上最大的收购之一,并可能在未来几年产生全球影响,包括可能影响数十亿人使用社交媒体的方式。

4月份以来,这位世界首富——也是该网络最强大的用户之一,拥有8100多万粉丝——通过监管文件、推文和在TED会议上的采访表明了他对这个有影响力的社交媒体的看法,以及如果他要成功收购将会做什么。

以下是他近期说过的一些改革方向:

软化其对内容审核的立场

这是最为重要的一个改革。

马斯克重申他推动软化推特在内容审核方面的立场,他在宣布该交易的新闻稿中说:“言论自由是民主运作的基石,而推特是一个数字城镇广场,人们在这里讨论对人类未来至关重要的问题,”

他在推特上写道:“我希望即使是我最糟糕的批评者也能留在推特上,因为这就是言论自由的意义。”

自称“绝对言论自由主义者”的马斯克早前多次批评推特在决定是否删除推文或永久禁止用户时应该更加谨慎。他还表示,该平台应遵守其提供服务所在国家/地区的法律。他还说,当推特确实做出改变以扩大或减少推文的影响力时,它应该让用户了解发生了什么。

马斯克没有公开评论他将如何处理前总统被禁止的推特账户。作为推特最引人注目的举措之一,去年它以“煽动暴力”的风险为由禁止了可能是其最强大用户的美国前总统特朗普。当时马斯克评论到:“很多人会对西海岸高科技作为言论自由事实上的仲裁者感到非常不满。”

不过特朗普在今天告诉福克斯新闻,他不会重新加入推特,他将按计划在接下来的7天内正式加入自己的TRUTH Social。

“这是我发声的平台。TRUTH Social是我发声和支持者的平台,”特朗普说。“但我希望每个人都能接受真相——保守派、自由派等等。”同时特朗普否认自己和马斯克有竞争,他说:“我认为这很好。我们希望我们的国家有自由、正义和公平,我们越开放越好。”

为推文创建编辑功能

推特用户长期以来一直要求使用编辑按钮。4月初,马斯克对推特用户是否想要这个功能进行了民意调查。400多万人投票超过70%表示同意。推特后来表示,自去年以来,它就一直在开发一个编辑按钮。

马斯克在TED采访中重申了他对编辑按钮的支持,并思考了该平台可能实现该功能的方式。

将上市公司私有化

马斯克在一份监管文件中表示,他希望将推特私有化。

“推特需要转变为一家私营公司,”他说。“推特具有非凡的潜力,我会发掘它。”

将推特撤出上市证券交易所,可能会让马斯克更容易对公司实施他想要的改革,因为大部分的股东压力都会消失。不过,马斯克在TED采访中也表示,如果他成功将公司私有化,他希望尽可能多地保留股东。

马斯克早前在推特上发布他打算以每股420美元的价格将特斯拉私有化,结果让他遭到了美国证券交易委员2000万美元的罚款。周一,他在推特上再次表达对该机构的不满,称其是“华尔街卖空者的傀儡”。

使算法开源并将代码放到GitHub上

在TED的采访中,马斯克建议将推特的算法开源,这意味着公司之外的其他人都将能够查看并推荐修改。他说,实现这一点的一种方法是将代码放在GitHub上,这是一个用于存储软件项目的网站。

给推特付费用户身份验证符号

马斯克在4月的第二个周末发布了一系列推文,建议推特给付费购买Twitter Blue的用户提供一个标记,以表明他们的帐户已“通过身份验证” 。Twitter Blue是推特于2021年6月推出的第一项付费订阅服务,按月订阅的用户可体验更多独特高级功能。

此外,马斯克还提出Twitter Blue订阅费用应该降至大约每月2美元,费用"应该与负担能力相匹配,并以当地货币计算"。他还提出了用狗狗币付费的选择。

减少依赖广告

广告几乎是推特的全部收入来源。

马斯克说,推特多年来一直试图在平台上推广它所谓的更健康的言论,在一定程度上增加了内容限制,部分原因是它对商业有好处。

马斯克表示,推特应该转向更加依赖订阅的商业模式。他建议为订阅账户删除所有广告。马斯克补充道:“这项服务不应该有广告。如果推特依靠广告收入生存,那么企业支配政策的权力就会大大增强。”

裁员减薪

马斯克还提出裁员并关闭公司在旧金山的总部。如果该公司要摆脱广告,很可能需要进行此类削减。马斯克于4月18日在推特上还表示,如果他的竞标成功,董事会将不会获得任何薪水,并补充说,此举每年将节省约300万美元。

马斯克4月初还提出了一个奇怪的想法,将推特的旧金山总部改造成无家可归者收容所——这一想法竟然还得到了死对头亚马逊创始人杰夫·贝索斯的认可——并有超过90%网友支持。

尝试阻止垃圾邮件和诈骗机器人

马斯克表示,他的首要任务是消除推特上的“机器人大军”,这些“机器人大军”会发送垃圾账户并进行诈骗。

在本月马斯克提出收购推特之前,他对平台的相关性表示担忧。

本月,有人统计了10 个最受关注的 Twitter 账户列表,马斯克看到并回应:“这些‘顶级’账户中的大多数人很少发推文,即使发帖发布的实质内容也很少。推特要死了吗?”他在随后的推文里解释,比如贾斯丁·比伯今年只发了一次推。

允许更长的推文

在4月15日的一条推文中,马斯克提出了“长推文”这一概念。

当时社交媒体公司Reddit的前首席执行官黄一山(Yishan Wong)在一篇冗长的推特帖子里提出了他对推特收购案的看法。马斯克没有回应黄一山的意见,却回应了他所采取的形式。“我从这篇短篇小说中得到的最直接的收获是,推特对于长推文来说是‘太迟了’”!他说。

在大多数情况下,推文最多可以包含280个字符,是之前140个字符的两倍。

未知的未来

很难确定以上这些马斯克的言论有多认真,因为其中一些推文似乎是为了幽默。比如马斯克询问了他的推特粉丝“删除推特中的w?”,他的粉丝中有近 57%的人投了“是”,而其余的人投了“当然”。也不能保证马斯克会对平台做出任何重大改变,毕竟,他还需要管理特斯拉和航空航天公司 SpaceX。

目前尚不清楚谁将带领公司向前发展。在报价文件中,马斯克告诉推特董事会:“我对管理层没有信心。”

据路透社报道,推特现任领导人帕拉格·阿格拉瓦尔周一告诉员工,推特的未来是不确定的。他说:“一旦交易完成,我们不知道平台会朝哪个方向发展。”

另外对于很多人来说,可能还有一个新问题:马斯克是否会将推特的总部从加州搬到德克萨斯,就像他对特斯拉所做的那样?



A股发生惨烈踩踏 背后是谁在撤退?

今天A股整体上跌幅中位数是8.5%,2400股跌幅在8.5%以上,3000股跌幅在7%以上,幸好,几百ST股涨跌幅限制在5%,不然,这个数据会更加令人绝望。 



今年以来,除了煤炭和黄金指数实现正面涨幅之外,其他所有板块,全军覆没。


5G通信主题ETF下跌40.54%,芯片ETF下跌39.19%,新能源汽车产业ETF下跌36.7%,无论是成长性行业,还是传统价值股,在这惨烈的行情下,无一幸免于难。

这个时候,我想起一句话:抱最好的希望,做最坏的打算。

人民币对美元汇率,继续暴跌,有数据显示,一季度的在华投资降低超过35%,美联储准备加息,加息再加息,加息。结果是什么?美元升值,热钱流出,完了,还有缩表。

我们已经看到,美元计数的港股资产,跌的亲爹都不认识了,中概股更是十八层地狱,腾讯又跌回330港元以下了。



同样的戏码,现在开始轮到A股,从最开始的高估值赛道股,再到医疗、白酒,到现在泥沙俱下,踩踏时有发生,无数赌博玩家不得不爆仓了。

可以预计,四月份,中国的经济心脏——上海,其经济数据的低迷,可能会超预期。从三月份的情况已经露出端倪。

3月,上海的消费品零售总额,创23个月新低,工业增加值同比大降近11%。四月份会怎么样?可想而知。

没有最糟,只有更糟。

A股有个规律,一旦它开始坠落,泥沙俱下,到某个整数点位必破,之前3200守不住,现在3000点也守不住了。

回望2008年,保卫沪指3000点,喊得震天响,时候看,那就是螳臂当车,3000点不但没守住,后来一度跌到1820点。

恐慌情绪一旦放大,任何基于基本面的分析,短时间内会被马上扔进垃圾桶。

虽然价值终究会回归,但短期内永远不要低估极端的限度。

有人说,今天的暴跌,是疫情引发的。

其实不对,奥密克戎的遍地作妖,也不是一天两天的事,我们在今年年初就不知不觉进入熊市。

大家看看下面的图表——




今年以来,除了煤炭和黄金指数实现正面涨幅之外,其他所有板块,全军覆没。

5G通信主题ETF下跌40.54%,芯片ETF下跌39.19%,新能源汽车产业ETF下跌36.7%,无论是成长性行业,还是传统价值股,在这惨烈的行情下,无一幸免于难。

这个时候,我想起一句话:抱最好的希望,做最坏的打算。

人民币对美元汇率,继续暴跌,有数据显示,一季度的在华投资降低超过35%,美联储准备加息,加息再加息,加息。结果是什么?美元升值,热钱流出,完了,还有缩表。

我们已经看到,美元计数的港股资产,跌的亲爹都不认识了,中概股更是十八层地狱,腾讯又跌回330港元以下了。

同样的戏码,现在开始轮到A股,从最开始的高估值赛道股,再到医疗、白酒,到现在泥沙俱下,踩踏时有发生,无数赌博玩家不得不爆仓了。

可以预计,四月份,中国的经济心脏——上海,其经济数据的低迷,可能会超预期。从三月份的情况已经露出端倪。

3月,上海的消费品零售总额,创23个月新低,工业增加值同比大降近11%。四月份会怎么样?可想而知。

经济不及预期,封城也不知何时到头。小鹏说了,如果上海再封一个月,所有的整车都无法进行,虽然特斯拉开始复工,但是复工率,也是惨不忍睹。

没有上海港口的重新开放,长三角的上游产业链,随时会有大量企业面临失血。

投资,本质上是对预期的押注。而现在问题在于,满屏幕,能为良好预期提供注脚的消息,少得可怜。

周末北京疫情发酵,5月初美联储缩表,人民币大幅贬值,恒瑞医药、三一重工、新华保险等机构股业绩下滑,阳光电源外部董事失联,宁德时代推迟披露2022年第一季度报告,本周又是五一前最后一周,也是一季报的最后一周,市场是雪上加霜。

一句话,在时代的山呼海啸面前,个体如沙,再怎么扑腾,其效果都是要大打折扣的。

放弃一步登天的幻想,正视空前严峻的现实(你不知道现在一旦失业,要找份好工作有多难),务必避免实际返贫,避免来之不易的家庭积累灰飞烟灭,广积粮、高筑墙,活下来,等风来。

没有一个冬天不可逾越,没有一个春天不会来临。



Leetcode discuss: 234. Palindrome Linked List

 April 25, 2022

Here is the link. 


C# | Optimal solution O(N) time complexity O(1) space

April 25, 2022
Introduction
It is important to learn optimal solution from Leetcode premium solution. I came cross the optimal solution and idea from here.

Optimal solution ideas | StefanPochmann
Solution 1: Reversed first half == Second half?

Phase 1: Reverse the first half while finding the middle.
Phase 2: Compare the reversed first half with the second half.

Leetcode premium discuss
Approach 3: Reverse Second Half In-place
Intuition

The only way we can avoid using O(n)O(n) extra space is by modifying the input in-place.

The strategy we can use is to reverse the second half of the Linked List in-place (modifying the Linked List structure), and then comparing it with the first half. Afterwards, we should re-reverse the second half and put the list back together. While you don't need to restore the list to pass the test cases, it is still good programming practice because the function could be a part of a bigger program that doesn't want the Linked List broken.

Algorithm

Specifically, the steps we need to do are:

  1. Find the end of the first half.
  2. Reverse the second half.
  3. Determine whether or not there is a palindrome.
  4. Restore the list.
  5. Return the result.

To do step 1, we could count the number of nodes, calculate how many nodes are in the first half, and then iterate back down the list to find the end of the first half. Or, we could do it in a single parse using the two runners pointer technique. Either is acceptable, however we'll have a look at the two runners pointer technique here.

Imagine we have 2 runners one fast and one slow, running down the nodes of the Linked List. In each second, the fast runner moves down 2 nodes, and the slow runner just 1 node. By the time the fast runner gets to the end of the list, the slow runner will be half way. By representing the runners as pointers, and moving them down the list at the corresponding speeds, we can use this trick to find the middle of the list, and then split the list into two halves.

If there is an odd-number of nodes, then the "middle" node should remain attached to the first half.

Step 2 uses the algorithm that can be found in the solution article for the Reverse Linked List problem to reverse the second half of the list.

Step 3 is fairly straightforward. Remember that we have the first half, which might also contain a "middle" node at the end, and the second half, which is reversed. We can step down the lists simultaneously ensuring the node values are equal. When the node we're up to in the second list is null, we know we're done. If there was a middle value attached to the end of the first list, it is correctly ignored by the algorithm. The result should be saved, but not returned, as we still need to restore the list.

Step 4 requires using the same function you used for step 2, and then for step 5 the saved result should be returned.

Things to argue | end of first half | Why it is better to choose end of first half?
It is interesting to learn that function design is to find end of first half node instead of the first node in second half. I will think about more later.

The following C# code passes online judge.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _234_palindrom_linked_list
{
    class Program
    {
        public class ListNode {
            public int val;
            public ListNode next;
            public ListNode(int val=0, ListNode next=null) {
                this.val = val;
                this.next = next;
            }
        }

        static void Main(string[] args)
        {
        }

        /// <summary>
        /// Code review:
        /// Study code: premium Leetcode solution 
        /// </summary>
        /// <param name="head"></param>
        /// <returns></returns>
        public bool IsPalindrome(ListNode head)
        {
            if (head == null)
            {
                return true;
            }

            // Find the end of first half and reverse second half.
            var firstHalfEnd = endOfFirstHalf(head);
            var secondHalfStart = reverseList(firstHalfEnd.next);

            // Check whether or not there is a palindrome.
            ListNode p1 = head;
            ListNode p2 = secondHalfStart;

            bool result = true;

            while (result && p2 != null)
            {
                if (p1.val != p2.val) result = false;
                p1 = p1.next;
                p2 = p2.next;
            }

            // Restore the list and return the result.
            firstHalfEnd.next = reverseList(secondHalfStart);
            return result;
        }

        // Taken from https://leetcode.com/problems/reverse-linked-list/solution/
        private ListNode reverseList(ListNode head)
        {
            ListNode prev = null;
            ListNode curr = head;
            while (curr != null)
            {
                ListNode nextTemp = curr.next;
                curr.next = prev;
                prev = curr;
                curr = nextTemp;
            }

            return prev;
        }

        /// <summary>
        /// using two pointers - one fast, one slow
        /// if the length of linked list is odd, then return ?
        /// if the length of linked list is even, then return ?
        /// 1 -> 2 -> 3 -> 4
        /// return second node with value 2
        /// 1 -> 2 -> 3 -> 4 -> 5
        /// return third node with value 3
        /// The node in the linked list is the first half end, make sure that it is correct
        /// in the above two test cases. 
        /// </summary>
        /// <param name="head"></param>
        /// <returns></returns>
        private ListNode endOfFirstHalf(ListNode head)
        {
            ListNode fast = head;
            ListNode slow = head;

            // check if fast's next two steps - null pointer 
            while (fast.next != null && fast.next.next != null)
            {
                fast = fast.next.next;
                slow = slow.next;
            }

            return slow;
        }
    }
}


Leetcode discuss: 234. Palindrome Linked List

April 25, 2022

Here is the link. 


C# | Review of my practice in July 2018

April 25, 2022
Introduction
I like to review my practice in July 2018. In order to determine if the linked list has palindrome feature, with O(N) time complexity and O(1) space.

Get length of linked list | O(N) time complexity
I wrote a function to get linked list length. Is it possible to solve the problem without getting length of linked list?

I also wrote a function to reverse a linked list. I do believe that the optimal solution may not need to save a copy of linked list using reversed order, which is not O(1) space.

Better solution | Code review
I like to write a solution using reverse first half of linked list, and then find middle point of linked list, compare first half and next half. The idea can be implemented using O(N) time and O(1) space complexity.

I do think that it is important to learn a few ideas and try a few things. Make a few mistakes, and I will learn much fast.

The following C# code passes online judge.

public class Solution {
    public bool IsPalindrome(ListNode head)
        {
            if (head == null)
                return true;

            if (head.next == null)
                return true;

            var length = getLinkedListLength(head);
            var isEven = length % 2 == 0;
            var back = head;
            var front = getNodeGivenK(head, length/ 2);
            if (!isEven)
            {
                front = front.next;
            }
            
            var reversed = reverseLinkedList(front);

            // compare two linked list one element a time 
            var index = 0;
            while (index < length / 2)
            {
                if (back.val != reversed.val)
                    return false;

                back = back.next;
                reversed = reversed.next;
                index++;
            }

            return true;
        }

        /// <summary>
        /// reverse a linked list using O(N) time and O(1) space
        /// </summary>
        /// <param name="head"></param>
        private static ListNode reverseLinkedList(ListNode head)
        {
            if (head == null)
                return null;
            if (head.next == null)
                return head;

            var next = head.next;
            var reversed = reverseLinkedList(next);

            head.next = null;
            next.next = head;

            return reversed;
        }

        private static ListNode getNodeGivenK(ListNode head, int k)
        {
            if (k == 0)
                return head;

            int index = 0;
            var iterate = head;
            while (index < k)
            {
                iterate = iterate.next;
                index++;
            }

            return iterate;
        }

        private static int getLinkedListLength(ListNode head)
        {
            if (head == null)
                return 0;

            var index = 0; 
            var iterate = head;
            while (iterate != null)
            {
                index++;

                iterate = iterate.next;
            }

            return index; 
        }
}

Leetcode discuss: 324. Wiggle Sort II

April 25, 2022

Here is the link. 

C# | Using Sorting to find median value

April 25, 2022
Introduction
It takes me less than 30 minutes to learn a C# algorithm. I chose to take less optimal time complexity algorithm, and tried to learn a few things through the practice.

The following C# code passes online judge.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _324_wiggle_sort_II___Sorting
{
    class Program
    {
        static void Main(string[] args)
        {
            var nums = new int[] { 1, 5, 1, 1, 6, 4 };
            WiggleSort(nums);
        }

        /// <summary>
        /// study code:
        /// https://leetcode.com/problems/wiggle-sort-ii/discuss/77696/c-n(log(n))-easy-understand-solution-without-using-virtual-index-and-three-way-partition.
        /// Time complexity: O(NlogN)
        /// Space complexity: O(N)
        /// </summary>
        /// <param name="nums"></param>
        public static void WiggleSort(int[] nums)
        {
            if (nums.Length <= 1)
            {
                return;
            }

            // Sort the array, so it is easy to find median value
            Array.Sort(nums);

            // Learn C# Array.Clone() API 
            var copy = nums.Clone() as int[];

            var median = nums.Length % 2 == 0 ? (nums.Length / 2 - 1) : (nums.Length / 2);

            // refer to https://discuss.leetcode.com/topic/41464/step-by-step-explanation-of-index-mapping-in-java
            for (var i = 0; i < nums.Length; i++)
            {   // i is even - any number less than median
                // i is odd - any number is bigger than median
                nums[i] = i % 2 == 0 ? copy[median - i / 2] : copy[nums.Length - 1 - i / 2]; 
            }
        }
    }
}