babalearn

babalearn

C# Unit Test

Unit test is an automated test that verifies if a single unit of a program works as expected. A single unit is the smallest piece of code that can be logically isolated in a program. Usually. it is a single…

.NET

C# vs .NET C# is not the only language we can run under .NET, but in 99% of the cases it runs under .NET. .NET is a framework that enables running applications wrtitten in C#. Some other .NET-compatible programming languages…

LINQ (Language Integrated Query)

LINQ is a set of technologies that allow simple and efficient querying over different kinds of data. It allows filtering, ordering and transforming the collection elements, and more. LINQ can work with other types of collections like databases or XML…

GraphQL

What’s the problem with REST? What is GraphQL? Steps to work with GraphQL Schema Schema is used to define the shape of data. It also defines the queries that we can run against this data and also to define mutation…

C# Learning Note

Solution vs Project A solution is simply a collection of projects. DLL DLL stands for Dynamic Link Library. The executable file (exe file) uses the DLL file. So if we remove the DLL, the *.exe would no longer work. Any…

java不同的版本对比

Java 8 vs Java 7 Java 8 相比 Java 7 是一次革命性的升级,它引入了函数式编程的思想。以下是 5 个最核心的新特性及代码对比,帮助你直观理解: 1. Lambda 表达式 (Lambda Expressions) 这是 Java 8 最具标志性的改动。它简化了匿名内部类的写法,使代码更简洁。 2. Stream API Stream API 极大地改变了处理集合(Collection)的方式,将以往繁琐的 for 循环和条件判断转化成了声明式的链式调用。 3. Optional 类 为了解决令人头疼的 NullPointerException (NPE),Java 8 引入了 Optional 容器,强制开发者显式地处理空值。…

Python

一些设计理念 基本数据类型 字符串 整数int 浮点数float 布尔类型bool 空置类型 NoneType 用type确定数据类型 条件语句 if的用法 数据结构 list list.append(element): 将元素加到最后listinsert(position, element): 将元素加到特定位置del list[0]:将在特定位置上的元素删除list.pop(): 将最后一个元素删除并返回list.pop(position): 将特定位置上的元素删除list.remove(element): 将列表里出现的第一个该元素删除list.sort()(reverse=True): 将原始列表排序sorted(list): 排序但是不影响原来列表内部元素顺序list.reverse(): 列表反转,会影响元素顺序len(list): 返回列表长度list comprehension: my_list = [val * 3 for val in range(1, 10)]list slice: copy_list = original_list[:]…

系统设计总结

系统设计的题目,感觉不用做非常多,但要善于总结,相似的问题可以总结成一类,用一个模板来解答,然后加一些小细节加以区分。 如何将一个信息同时传播给多个关注该事务的client 定时任务 Event Sourcing Event sourcing就是说我们不存最后的状态,而是把每一个action都存起来,然后需要结果的话,我们得到所有的action,然后derive出最后的状态是什么。那什么时候需要event sourcing呢?跟钱打交道的时候,比如说Auction system需要我们prove为什么是这个人赢得了最后的bid等。另一个就是,如果很多人都在竞拍一个产品,如果我们只存最终结果(在这种情况下是update Auction表里的winner),在高并发的情况下会一直修改同一个数据,这要比往bid table里不停append给bid table的的性能要差很多。 Lease机制 Lease机制是facebook设计memcached的时候,为了解决数据一致性的问题解决的,非常适合高并发场景。那么是如何工作的呢?– 同一个cache key,memcached维护一个当前有效的lease token,不管多少请求都拿到这个token– server A和server B都来读取数据,拿到相同token,然后server A先过来更新– Memcached对比了token,有效,就把对应的值改掉,lease token也发生了变化– server B再过来更新的时候因为token invalid,所以更新被拒绝 确保任务不会被重复执行 这是经典的分布式系统并发控制问题。例如,a cluster of worker nodes会不停的扫描数据库来发现下一个要做的task。如何保证node A take该task之后,别的node不会重复去执行呢? 这是悲观锁方案 这是乐观锁方案,用的CAS, 基于version。也可以基于时间戳,设一个last_modified column。 两个方案各有利弊。如果是高冲突环境下,悲观锁比较好,skip locked可以避免无效等待,每次查询基本都可以获得结果,不需要不停的retry。低冲突环境下,乐观锁比较好,没有锁开销,并发读取效率比较高。…

JavaScript

JS特性 JavaScript代码能放在哪里? 也可以将javascript代码写入一个外部的js文件当中,然后通过script标签进行导入。写入外部文件的好处是不同的html页面都可以引用它,也可以利用到浏览器的缓存机制。是比较推荐的一种方式。 注意:script标签一旦用于引入外部文件,就不能在标签体里面写js代码了,即使写了浏览器也会忽略的。如果头铁,非得写,那就在新建一个script标签。 注释 注释有单行注释或者多行注释 数据类型 六种基本的数据类型: 前五种为基本数据类型,而Object属于引用数据类型。 判断一个变量的类型,可以用typeof来检查。 Number.MAX_VALUE -> JS数字的最大值,超过该值就会显示Infinity。NaN -> Not a number,如果用typeof来检查,还是会显示为number。Number.MIN_VALUE -> 0以上的最小的数字,表示正的最小数。null专门用来表示一个为空的对象。如果用typeof检查,会返回“object”。当申明一个变量但不给变量赋值时就是undefined,用typeof检查就是”undefined”。 强制类型转换 将其他数据类型转换为String 将其他数据类型转换为Number 逻辑运算符 取反 ! 用!对非布尔值进行取反操作,则先将该值转换为布尔值,再进行取反操作。 与运算 对非布尔值进行运算的时候,会将其转换为布尔值,然后再运算,并返回原值 或运算 如果第一个值为true,则直接返回第一个值,如果第一个值为false,则返回第二个值 全等/不全等 ===和==类似,但不同的是全等不会做自动类型转换,如果类型不同,直接返回false。不全等同理。 流程控制语句 if…else if…else if…else 该语句中,只有一个代码块会被执行,一旦代码块执行了,则直接结束语句。 switch…case… 在执行时会依次将case后的表达式的值和switch后的条件表达式的值进行全等比较,如果比较结果为true,则从当前case处开始执行代码。当前case后的所有代码都会执行,因此我们可以在case的后面跟一个break关键字来跳出switch语句。如果比较结果为false,则继续向下比较。如果所有的比较结果都为false,则执行default里的语句。 对象…

Data Structure

TreeMap Heap PriorityQueue Insert() log(n) log(n) log(n) Delete() log(n) log(n) O(n) Pop() log(n) log(n) log(n) Find() log(n) log(n) Not support Modify() log(n) log(n) Not support Min / Max log(n) O(1) O(1) Upper / Lower log(n) Not support Not support Lower…

动态规划

什么题适合动态规划? 什么题不适合动态规划? 动规四要素 顺带回忆一下递归三要素 动态规划只能记录一种最优方案 练习题目:

HTML

什么是HTML? HTML: Hyper Text Markup Language. HTML tags HTML的标签分为两种:双标签(paired tag)和单标签(self-closing tag)。所谓的双标签,就是说该标签有一个起始标签(opening tag)和一个结束标签(closing tag),形式为:<起始标签>标签体</结束标签>。单标签的形式为:<标签名 />,其中foward slash是可以忽略不写的。 标签和标签之间的关系可以是并列,也可以是嵌套。如果是嵌套关系,编写代码的时候可以用tab键来进行缩进。 HTML的每个标签有其对应的标签属性(attribute),给标签提供一些附加信息。属性可以写在双标签中的起始标签里,或者单标签里。形式为: 有一些比较特殊的标签,只有属性名,没有属性值,例如: 注意事项: HTML的基本结构 在网页上点击鼠标右键,会出现查看网页源代码和检查。查看网页源代码看到的是程序员编写的源代码,检查看到的是经过浏览器处理后的源代码,一般来说日常开发中“检查”用到的居多。 网页的基本结构如下: HTML注释 注释的作用是对代码进行解释和说明,其内容会被浏览器所忽略,不会呈现到页面中,但源代码中依然可见。 HTML文档声明 告诉浏览器当前网页的版本,由于html5是向后兼容的,所以直接标注为html5版本就好,以前的版本声明写起来非常的麻烦,也没有必要。要将文档写在第一行,且在html标签的外侧。 HTML字符编码 常见的字符集有如下几种: 原则是:存储的时候,务必采用合适的字符编码,必须有汉字,就不能用ASCII或者ISO 8859-1字符集。存储是用的那种方式编码,读取的时候就用哪种方式解码,要不然就会出现乱码。平时编写代码的时候,统一采用UTF-8编码。 HTML设置语言 主要作用是让浏览器显示对应的翻译提示,且有利于搜索引擎的优化。具体写法: h标签 一种语义化标签。块级元素(block element)。h1~h6为标题。h1最好写一个,h2~h6能适当多写。h标签不能相互嵌套。 p标签 块级元素。代表一个段落。里面不能有别的块级元素。 div标签 块级元素。没有任何含义,是用来整体布局的一个标签,可以比喻为生活中的包装袋。 文本标签…

React Tutorial

为什么要学习React?换句话说,为什么原生js不够好呢? 虚拟DOM JSX语法规则 JS复习 组件 状态和展示 类中的方法this指向 现在的问题是当h1标签被click,调用回调函数changeWeather时,函数里的this为undefined,那该怎么办呢? 状态不可直接更改,需要调用setState({…})来更改,且更新是一种合并,不是替换。也就是说,同名的就替换了,不同名的保留。 如果不想写的这么麻烦,那可以用这种写法,完美解决this的问题 总结一下,state如果不依赖传参进来,直接写到class体内,然后方法也写成property的形式,并用箭头函数给其赋值。 对props进行限制: 组件的生命周期(旧版) 组件的生命周期(新版) 其实不管是新的生命周期还是旧的,常用的就3个: 三个废弃的钩子: 连续解构赋值加重命名:

SQL Query常见错误小结

NATURAL JOIN vs INNER JOIN 对比点 INNER JOIN NATURAL JOIN 是否需要指定连接条件 ✅ 需要使用 ON 明确指定连接字段 ❌ 不需要写 ON,自动匹配同名列 控制力 ✅ 高:你控制连接字段和方式 ❌ 低:自动决定,容易出错 可读性 ✅ 明确清晰,推荐生产使用 ⚠️ 可读性差,容易误用 常见用途 ✅ 实际项目中常用 🚫 学术/练习中偶尔出现,生产中少用或禁用 支持程度 ✅ 所有数据库都支持 ❌ 有些数据库(如 SQL Server)不支持…

Server-to-Server常用认证方式

API key 放在请求头里,例如 Token(jwt) 先调用登录api来获得token 然后得到相应: 然后带上这个token在请求头里: OAuth2 1. 用户点击“用 Google 登录” 2. 前端拿到 Google 的 ID Token 3. 前端把 ID Token 传给你的后端 API 4. 你的后端验证这个 Google ID Token 5. 后端根据用户身份生成自己的 JWT(可选) 6. 后端授权并返回资源 ✅ 具体技术细节 验证 Google ID Token…

Software Test

There’re different test types across software testing. Unit testing Unit testing means writting automated tests for the smallest testable parts of the code to check if they work as expected. Integration Testing Integration testing checks how different parts(modules) of the…

Leetcode 78: Subsets

The original question can be found here. The question is asking to return all subsets of an integer array, and each element in the array is unique. Recursion Normally the question which is asking for all possible solutions are searching.…

Leetcode 209: Minimum Size Subarray Sum

The original question can be found here. The question is asking us to the minimum length of subarray that it’s sum is no less than target value. Brute Force The brute force we can immediately think of is to enumerate…

Leetcode 74: Search a 2D Matrix

The original question can be found here. The question is asking us to find the target element in a 2-dimensional array. Each row is greater than the row above it. Within each row, elements are sorted as well. Brute Force…

Leetcode 35: Search Insert Position

The original question can be found here. The question is asking for a specific target value, if it exists in the array, return the index. Otherwise, return the index position that it should be inserted. Brute Force We just iterate…

Leetcode 162: Find Peak Element

The original question can be found here. It’s asking us to find an element that is grater than both left and right neighbors. Since it’s asking for a O(logn) solution, let’s directly jump to the binary search solution. There’re 4…

Leetcode 33: Search in Rotated Sorted Array

The original question can be found here. The question is asking us to find a target element in rotated sorted array. Since the question is explictly asking for a O(logn) solution, so let’s jump to the binary search solution. After…

Leetcode 852: Peak Index in a Mountain Array

The original question can be found here. The question is asking us to return a peak element, which means, an element is greater than both 2 adjacent neighbors. Brute Force Scan the array: Time complexity: O(n). Space complexity: O(1). Binary…

Leetcode 658: Find K Closest Elements

The original question can be found here. The question is asking to return the k closet elements to target x. The array is sorted. Heap First solution is using a heap to keep track the current closest elements to x.…

Leetcode 704: Binary Search

The original question can be found here. The question is asking us to perform a binary search on an sorted array, which is a classical algorithm. If you haven’t, please visit my another post which detailed describe this algorithm. Time…

Java Optional: Is It Useful?

Introduction Java Optional is a container class that encapsulates an optional value, which was introduced by Java 8. However, I still see lots of colleagues not leveraging it in their work. Maybe it’s too hard to use? Or maybe we…

Infer Algorithm By Time Complexity

Introduction Sometimes we need to infer algorithms by time complexity. After we bring out the brute force solution, the interviewer may ask: Can you solve by time complexity of O(xxx)? We don’t always know what’s the optimal solution, but we…

Binary Search

Introduction Binary search is a very popular algorithm for finding a target element in a sorted array. Algorithm Here’s a standard way for implementing this algorithm: Time complexity: O(logn). Space complexity: O(1). However, this implementation will be problematic if we…

Leetcode 509: Fibonacci Number

The original question can be found here. The question is asking to return the Nth fibonacci number. F(0) = 0, F(1) = 1 and F(n) = F(n – 1) + F(n – 2). The easist solution is using recursion. If…

Recursion

Introduction Recursion is a common topic during coding interview. This post talks about how to succesfully write a correct recursion solution for a coding exercise. Method signature Method signature we care in a recursion method is defined by all the…

Quick Select

Introduction Quick select is an algorithm used to quickly locate the Kth largest/smallest element in an array. Technically speaking, it’s part of quick sort algorithm. Algorithm Let’s say we want to find the Kth largest element in an array. Every…

Leetcode 912: Sort an Array

The original question can be found here. It’s asking us to provide O(nlogn)solutions. Quick Sort The algorithm can be found here in my another post. Merge Sort The algorithm can be found here in my another post.

Merge Sort vs Quick Sort

Introduction Both merge sort and quick sort are popular sorting algorithms. They both use recursion and leverage devide and conquer principles. But what are their differences? Time Complexity Merge sort has time complexity as O(nlogn), where n is the size…

Merge Sort

Introduction Merge sort is another popular algorithm, with time complexity as O(nlogn). It’s leveraging the principle of divide and conquer, and this is definitely the algorithm we should master. Algorithm We recursively split the array into 2 parts, sort them…

Quick Sort

Introduction Quick sort is one of the most popular algorithms for sorting. It’s not uncommon to see this algorithm or other algorithms(e.g. partition) leveraging the principle of quick sort during interview. Algorithm The idea behind quick sort is divide and…

Leetcode 1: Two Sum

This is probably our very first coding preparation question. The original question can found here. This question is asking us to return the indices of 2 elements such that their sum equals to the target number. Brute Force The brute…

Leetcode 680: Valid Palindrome II

The original question can be found here. The question is asking if a string is palindromic if at most 1 character can be removed from string. Let’s look at an example: Let’s say we still use the 2 pointers apporach…

Leetcode 125: Valid Palindrome

The original question can be found here. The question is asking for a given string, determine if it’s palindromic. Only alphanumeric letters are taking into account and letters’ cases are ignored. Reverse and Compare The first solution we can think…

Leetcode 5: Longest Palindromic Substring

The original question can be found here. There’re multiple solutions to this question, and we’ll start with the brute force one. Brute Force We enumerate all substrings and check if it’s palindromic. If yes, we update the final result by…

Interview Skills and Coding Styles

Here are some interview skills and coding styles that on top of my head. Interview Skills Try to avoid “I know a fancy algorithm called xyz…” Try to avoid using uncommon existing algorithms used for very specific cases, such as…