Cracking the Coding Interview: Q 16.5 Run Threads In Sequence
With the help of C++11 multi-threading library, this question about running threads in sequence can be easily implemented. Both implementations below is public on GitHub.
Question
Suppose we have the following code:
public class Foo {
public Foo() { ... }
public void first() { ... }
public void second() { ... }
public void third() { ... }
}
The same instance of Foo will be passed to three different threads. Thread A will call first, thread B will call second, and thread C will call third. Design a mechanism to ensure that first is called before second and second is called before third.
Fix UltiSnips Snippets Load Problem
To better use YouCompleteMe in Vim, I switched from SnipMate to UltiSnips, because UltiSnips can be integrated into YCM. I also installed snippets since UltiSnips offers an engine only.
Plugin 'SirVer/ultisnips' Plugin 'honza/vim-snippets'
But after I installed it with Vundle, it didn’t work correctly. It only load ‘all’ filetype snippets. You can check this by typing
:UltiSnipsEdit
in Vim. If only ‘all.snippets’ appears, then here is the problem.
Cracking the Coding Interview: Q 3.4 Hanoi Tower Problem
Although the Hanoi Tower problem very basic, by studying it, we learn thinking in recursive way. Generally we should analysis the base cases and reorganize the problem into subproblems. The different thing from 4th edition is that, in 5th edition, it’s required to use stack to simulate the tower.
Question
In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sizes which can slide onto any tower.The puzzle starts with disks sorted in ascending order of size from top to bottom (i.e., each disk sits on top of an even larger one).
You have the following constraints:
(1) Only one disk can be moved at a time.
(2) A disk is slid off the top of one tower onto the next tower.
(3) A disk can only be placed on top of a larger disk.
Write a program to move the disks from the first tower to the last using stacks.
Cracking the Coding Interview: Q 2.6 Circular Linked List Problem
Recently I’m working on “Cracking the Coding Interview 5th Edition”. All my solutions are public on Github. For this typical question about circular linked list in chapter 2, the way to solve it is a bit tricky.
Question
Given a circular linked list, implement an algorithm which returns node at the beginning of the loop.
DEFINITION
Circular linked list: A (corrupt) linked list in which a node’s next pointer points to an earlier node, so as to make a loop in the linked list.
EXAMPLE
Input: A -> B -> C -> D -> E -> C [the same C as earlier]
Output: C
Writing Proxy Lab for CSAPP
In contrast with previous malloc lab, proxy lab is not that hard in CSAPP. But proxy lab could be more complicated if you intend to make it robust. I met many weird problems when writing this lab. As a rookie to network programming, I cannot explain everything well, but I am able to offer some measures to solve problems.
Generally, writing a proxy at proxy lab level is relatively simple in algorithm. You proxy should act as a server when connecting to a client and act as a client when connecting to remote web servers. You may get some inspiration by reading source code of Tiny Proxy, which is included in proxy lab handout.
Like my previous articles about CSAPP, I will talk about some gists only.