二十七

二十七号,2017就这样转瞬即逝了,想起上个月的今天自己还在飞机上度过了二十七岁的生日,仿佛就在昨天。

今年还算是对得起自己,年初对自己许下的愿望大部分都实现了——换了工作,回了久违的东部,拥有了属于自己的房子,去了心心念念的欧洲。大部分之前都已经提过,这里主要写写八月份以后的事吧。

九月份爸妈来美国看我顺便收拾房子。妈妈之前来过,老爸这是第一次来,好像还挺喜欢这里的。我是迫不及待想让他们了解我在这边的生活,给老爸安排了很满的行程,基本上没什么休息的时间,走了一号公路除了big sur的全套,去LA见了小姑和他的好友,还有圣地亚哥。印象最深的是在环球影城的水世界表演,虽然我和妈妈之前都看过,但是被喷水的时候一家人还是都笑疯了,已经记不清我们仨上一次这样在一起放肆大笑是在什么时候,对于像我这样一个独自久了的人,那种家人的温暖真的很美好。有时候我会担心自己,担心自己就这样慢慢地丧失了关心他人的能力,变得越来越冷漠。

Read More

大半流水

许久没有更新。2017过去了一大半,想来应该要写点东西记录一下。

年初从国内回来接了三个面试,打起精神刷了刷题准备跳槽。还没来得及让我考虑,老东家就撑不住了。说实话我当时一点都不意外不着急,从开始的意气风发到无数次错失机会,最终走到今天这个地步,作为一个亲历者还是相信因果的。不能说是小胖一个人或者后面任何一个CEO的问题,整个公司有非常好的文化气氛,但无奈这是一个残酷的市场,只有惋惜。迟迟不愿离开,除了自己懒惰,也多少因为感情吧,第一个收留我的地方,教会我许多,见证我成长。好在大家都有不错的归宿,离场不散场,时不时还会出来一聚,就像从前一样。

入职新公司之前,回到东岸见了些朋友。毕业后再也没有来过这里,这次有机会当一个真正的游客走在纽约街头,却无法像游客那样从心出发欣赏这里的美。MOMA有知名画作,sleep no more刷新了我对舞剧的认识,还有长岛舒适的节奏,第一次住新泽西,比想象中要好很多。来美第一次坐灰狗去DC,却是帮朋友们搬了家。华府的樱花的确很美,各种博物馆也值得让我再去看一次。一切看起来都很好,但奇怪的是我内心却毫无波澜,从始至终没有像从前那样有让我惊艳的时刻,可能真的老了吧。

Read More

Interview Note - Union Find

Union Find

From wiki:
In computer science, a disjoint-set data structure, also called a union–find data structure or merge–find set, is a data structure that keeps track of a set of elements partitioned into a number of disjoint (nonoverlapping) subsets. It supports two useful operations:
Find: Determine which subset a particular element is in. Find typically returns an item from this set that serves as its “representative”; by comparing the result of two Find operations, one can determine whether two elements are in the same subset.
Union: Join two subsets into a single subset.

Example:
261. Graph Valid Tree
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.

For example:
Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], return true.
Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]], return false.

Hint:
Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], what should your return? Is this case a valid tree?
According to the definition of tree on Wikipedia: “a tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.”

Note: you can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class Solution {
public boolean validTree(int n, int[][] edges) {
UnionFind uf = new UnionFind(n);
for (int i = 0; i < edges.length; i++) {
if (!uf.union(edges[i][0], edges[i][1])) {
return false;
}
}
return uf.size == 1;
}

private class UnionFind {
int size;
int[] nodes;

UnionFind(int size) {
this.size = size;
this.nodes = new int[size];
for (int i = 0; i < size; i++) {
nodes[i] = i;
}
}

boolean union(int a, int b) {
int label_a = nodes[a];
int label_b = nodes[b];
if (label_a == label_b) {
return false;
} else {
for (int i = 0; i < nodes.length; i++) {
if (nodes[i] == label_a) {
nodes[i] = label_b;
}
}
size--;
return true;
}
}
}
}

Read More

26

2016即将过去了,这一年可能是我最近几年里最平淡最失败的一年。去年底给自己许下的两件事都没有完成,回过头来都不太记得这一年发生过什么。可能会宽慰自己说什么事都要厚积薄发,但真的到自己身上却又切实的感到害怕了。

好的一面是,这一年换了组,做了很有意思的项目,学到了很多东西,特别感谢Yeongmi让我了解了许多知识。中途搬了家,有一个新环境让自己更好的工作学习,同时享受生活。挖了象拔蚌,重游太浩湖,去了拉斯维加斯,这一年没怎么出门有点可惜了,很多时间都强行放松自己看综艺看视频。交了些新同事新朋友,周末时不时出来坐坐,寂寞倒是没有,但也没去年那么充实。

不好的一面是,一些同事离职了有些难过,毕竟日子不会一直安稳下去。以为进入了人生的新阶段,但原来每个阶段都要努力去维持。说实话我有时勤奋有时很懒,但总的来看懒的时候多,如果没有什么刺激我恐怕会一直懒惰下去。所以到了现在,年底了,意识到这一年浑浑噩噩的即将结束,才忽然紧张害怕起来,紧张时间不多了还有很多想做而没做的事,害怕明年会像今年一样在低气压里徘徊。

最近又有些让人失落的消息,尽管已经有了心理建设但还是不怎么开心,内心安慰自己没什么特别的你只是个凡人,一定会经历高峰和坠落,把事情看简单点不要有什么压力,轻松的心态通常都会有意想不到的效果。下周回国尽量调整自己,探亲叙友,来年精神饱满的完成今年未完成的心愿!

PowerMockito -- Mock and Spy

A bad system design can lead to much hard work. In order to increase the unit tests coverage, I recently started to work on writing unit tests for some classes. One of the case is I want to test a method as follow:

1
2
3
4
5
6
7
8
public final ReturnType getMethod (SomeRequest someRequest) {
AnotherRequest anotherRequest = new AnotherRequest(someRequest);
SomeResponse someResponse = SomeService.getInstance().someMethod(anotherRequest);
SomeValue someValue = someResponse.getValue();
/**
* Some processes with someValue..
*/
}

The main purpose of this test is testing the process with someValue, so I should just mock the .getValue() method. But the thing is not that easy. Let me put more related classes here:
SomeService.class

1
2
3
4
5
6
7
8
9
10
11
12
13
public final class SomeService {
private static SomeService instance = new SomeService();
static {
// A static block
}
protected SomeService() {}
public static SomeService getInstance() {
return instance;
}
public SomeResponse someMethod(AnotherRequest anotherRequest) {
// Implementation of the method..
}
}

SomeResponse.class

1
2
3
4
5
public class SomeResponse {
public SomeValue getValue() {
// Implementation of getValue()
}
}

SomeValue.class

1
2
3
4
5
6
7
8
9
public class SomeValue {
private String name;
private void populateValue(PreDefinedType preDefinedType) {
// Generate name from a preDefinedType, basically a black box.
}
public String getName() {
return name;
}
}

Read More