Wednesday, November 8, 2023

2476. Closest Nodes Queries in a Binary Search Tree | Leetcode discuss

Here is the link. 

C# | Avoid searching in O(N) height tree | Array.BinarySearch

781
0
24 minutes ago
C#

Nov. 8, 2023

Intuition

My first practice has TLE issue since 44/45 test case is a binary tree with height O(N), N is total number of nodes in the tree.

Approach

The approach is to work on the following two steps:

  1. Apply inorder traversal first, and then apply binary search C# Array.BinarySearch.
  2. BinarySearch - if not found, return negative value - bitwise complement - larger than the given value

Complexity

  • Time complexity:

O(N)

  • Space complexity:

Code

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class Solution {
     /// <summary>
        /// Nov. 8, 2023
        /// Avoid unbalanced tree - O(N) - tree height is same as O(N), N total number of nodes in the tree
        /// Save binary tree in a sorted array first, and then apply binary search. 
        /// 
        /// </summary>
        /// <param name="root"></param>
        /// <param name="queries"></param>
        /// <returns></returns>
        public IList<IList<int>> ClosestNodes(TreeNode root, IList<int> queries)
        {
            var sorted = new List<int>();
            applyInOrder(root, sorted);

            var result = new List<IList<int>>();
            if (root == null)
            {
                return result; 
            }

            foreach (var number in queries)
            {
                var found = sorted.BinarySearch(number);

                if (found > -1)
                {
                    result.Add(new int[] { sorted[found], sorted[found] });
                }
                else
                {
                    var index = ~found;
                    var smallestLarger = -1;
                    var largestSmaller = -1;
                    if (index < sorted.Count)
                    {
                        smallestLarger = sorted[index];
                        if (index > 0)
                        {
                            largestSmaller = sorted[index - 1];
                        }
                    }
                    else
                    {
                        largestSmaller = sorted[sorted.Count - 1]; 
                    }

                    result.Add(new int[]{largestSmaller, smallestLarger}.ToList()); 
                }
            }

            return result;
        }

        private void applyInOrder(TreeNode root, IList<int> list)
        {
            if (root == null)
            {
                return; 
            }

            applyInOrder(root.left, list);
            
            list.Add(root.val);

            applyInOrder(root.right, list);
        }
}


Tuesday, November 7, 2023

Rest API (Intermediate) Skills Certification Test | Hackerrank.com

 

Rest API (Intermediate) Skills Certification Test

Verify your Rest API Skills. Accelerate your Job Search.

Which database supports sharding?

Which database supports sharding?
Cassandra, HBase, HDFS , MongoDB and Redis are databases that support sharding. Sqlite, Memcached, Zookeeper, MySQL and PostgreSQL are databases that don't natively support sharding at the database layer. For databases that don't offer built-in support, sharding logic has to reside in the application.

Sharding | system design interview concepts | IGotAnOffer

 

Sharding: system design interview concepts (7 of 9)


Here is the article. 

If you want to succeed in system design interviews for a tech role, then you’ll probably need to understand sharding and when to use it within the context of a larger system.

Sharding is essentially the horizontal scaling of a database system that is accomplished by breaking the database up into smaller “shards”, which are separate database servers that all contain a subset of the overall dataset.

This is just a high-level definition, so to fill in the gaps we’ve put together the below guide that will walk you through sharding and how you should approach it during a system design interview. Here’s what we’ll cover:

  1. Sharding basics
  2. Approaches to sharding
  3. Example sharding questions
  4. System design interview preparation

Monday, November 6, 2023

System design interview prep (6 steps to an offer at FAANG)

Here is the article. 

“错误”的行为

作者: [美] 理查德·塞勒
出版社: 中信出版社
副标题: 行为经济学的形成
原作名: Misbehaving: The Making of Behavioral Economics
译者: 王晋
出版年: 2018-3-1
页数: 456
定价: 69.00
装帧: 精装
丛书: 理查德·塞勒三部曲

ISBN: 9787508684512 

准备好改变你对经济学的看法了吗?

纵观理查德·塞勒的职业生涯,我们发现他的研究始终围绕着一个激进的观点开展:经济活动的主体是人,即拥有可预测行为且容易犯错的个体。在本书中,塞勒讲述了他将经济学从高高在上的“象牙塔”中带回现实的艰难之旅,其中的故事引人入胜,并且不乏诙谐幽默,彻底改变了我们对经济学、对自己以及对整个世界的看法。

传统经济学的假设前提是,经济活动的主体是理性的经济人。研究伊始,塞勒就意识到人类与像《星际迷航》中的斯波克那样没有情感的理性人完全不同。不管是购买闹钟、转售篮球门票,还是申请抵押贷款,我们都会存在某种偏见,所做出的决定与经济学家假设的标准理性模型相去甚远。换句话说,我们的行为并不理性,甚至在传统经济学家看来是“错误”的。更重要的是,这种“错误”的行为会导致严重的后果。起初,经济学家并不屑于研究人们的错误判断及其对市场的影响,他们认为这只是一种引人发笑的“小伎俩”,无足轻重。不过,如今这些关于人类行为的研究却帮助我们在工作和生活中做出了更好的决定,也促使政府制定出更有效的政策。

本书点缀着塞勒与传统经济学思想激烈交锋的有趣故事,以独一无二的方式探索了人类深层次的弱点。当经济学遇到心理学,碰撞出的火花将对个人、管理者和决策者产生深远和富有启发性的影响。

理查德·塞勒(Richard H. Thaler)

 理查德·塞勒(Richard H. Thaler)

生于1945年,1974年毕业于罗切斯特大学,获经济学博士学位。他目前在芝加哥大学布斯商学院执教,任金融和行为科学教授及行为决策研究中心主任;此外,他还在美国国民经济研究局(NBER)主持行为经济学的研究工作。

塞勒教授的研究主要集中于社会心理学、行为经济学等交叉学科。他被公认为行为经济学和金融学领域的先驱。

2015年,理查德•塞勒当选美国经济学学会主席。

2017年,因对“行为经济学”的贡献,理查德•塞勒被授予诺贝尔经济学奖。

其主要著作还包括《赢家的诅咒》,以及与卡斯•桑斯坦合著的畅销书《助推》。