March 31, 2016

Design Pattern Overview - Chain of Responsibility


Chain of Responsibility is a design pattern to pass responsibility to other objects in a chain. This simple definition is a little bit confusing, in other words, sometimes you send a message to one object, that object can’t handle, so the object ask another object to help, usually with same type. And the responsibility can pass one by one recursively. It looks very similar with a linked list, one node can have a next pointer which point to another node, until the next pointer is null.

March 28, 2016

Design Pattern Overview - Prototype


Prototype Pattern is a creational pattern to create new objects by cloning existed objects. Usually we use consutructor of class to create new instances, but you can also clone one from old object, then copy all the fields.

March 23, 2016

Working with Timezone

I’m always relying Ruby on Rails to build web application, I think it’s not enough for a developer because Rails has already handled all the things actually I need to know how to deal with by myself.

Since this project I’m doing during my summer internship, I decide to create web service with Sinatra framework, which is a very simple micro framework to build web application. Sinatra provide limited features for you. There are many details you need to notice if you switch from Rails.

October 15, 2015

Bloom Filter Intro

Bloom Filter 用来快速查找某个元素在不在集合中的算法,但不保证100%正确,有一定概率的误判(False Positive), 也就是判断结果是在集合中的话,存在一定几率其实元素不在集合中。但是不会又漏判(False Negative),就是判断结果不在几何中的话,那这个元素肯定不在集合中。

September 8, 2015

LeetCode - Find the Celebrity

Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity. The definition of a celebrity is that all the other n - 1people know him/her but he/she does not know any of them.

Now you want to find out who the celebrity is or verify that there is not one. The only thing you are allowed to do is to ask questions like: "Hi, A. Do you know B?" to get information of whether A knows B. You need to find out the celebrity (or verify there is not one) by asking as few questions as possible (in the asymptotic sense).

September 8, 2015

LeetCode - First Bad Version

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have n versions 1, 2, …, n and you want to find out the first bad one, which causes all the following ones to be bad.

August 4, 2015

Explained OAuth 2 by Example - Part I

OAuth 2 Introduction

Authorization system is a very fundamental component for most websites or applications, we must login first before we can access the resources we own. I believe you still remember about 10 years ago, you need to create your account for every website, you have a Google account, a Yahoo account, and a MySpace account. What is changed right now, obviously, you can login many websites with your Google account or Facebook account, you don’t need to create new account any more. The hero behind is OAuth 2, a protocol enable third-party application obtain limited access to an HTTP service.

July 14, 2015

Ruby You Should Know - Top level

Why Ruby can be scripting language?

Before I start writing Ruby, my main language is Java. The most verbose thing in Java world is everything should be defined in classes. You need to have a class and a static main function even you just want to print a hello world. You can’t directly write or call a function globally with Java, that’s why Java can’t become a scripting language. If you have Ruby in hand, the only thing need to do is leave a line puts 'hello world'. I think the top level function definition and invocation is the most important reason why Ruby can be a perfect scripting language.

April 3, 2015

LeetCode - Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S.
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, “ACE” is a subsequence of “ABCDE” while “AEC” is not).

Here is an example:
S = “rabbbit”, T = “rabbit”
Return 3.

March 21, 2015

Points of C++ - lambda


void print_elem(const int x) {
cout << x << " ";