17 Jan 2014
####Google
Pay attention to Bit Manipulation
####General
Scalability
####Build a strong network
- Meetup.com or alumni network
- Be open
- LinkedIn
- Be helpful
####Compile a preparation grid
See Page.40
####Power of 2 table
See Page.47
####5 Steps to a technical question
- Resolve ambiguity
- Design an algorithm
- Write pseudocode first(tell about it)
- Write code at moderate pace
- Test and fix
####5 Algorithm approaches
- Examplify
- Pattern matching
- Simplify and generalize
- Base case and build
- Data structure brainstorm
##Interview Questions
###Data Structures
###1. Arrays and Strings
#####Hash Tables
Implementing a hash table…
#####ArrayList(Dynamically Resizing Array)
#####StringBuffer
Implement your own version of StringBuffer
#####1.1
- Forgot the fact that if the string contains unique chars, then the length should be length of alphabet
- Need to familiaze with bit manipulation
#####1.3
- For string equal, use String.equals(anotherStr), == only determines if it is identical
- If possible, try to cut out some temporary variables
- If one is permutation of another, they must have the same length
- Always think of some property to minimize the computation
- The pattern to sort an string: toCharArray then sort
- Forget about case sensitive and whitespace
#####1.4
- Watch out about i++ etc
- I really don’t know that I’ll have the true length of the string(But it says it in the question)Read!!
- Starting backwards is really a excellent idea.
#####1.5
- Forget about the last sentence: the addition case
- As long as there is loop, watch out for array out of index!
- An alternative to StringBuffer would be declare the char array with enough space upfront
- Review the countCompression when possible
#####1.6
- It is not neccessary to use recursion
- It’s easy to fall into the thinking pattern that every index, position starts with 0
- It’s actually matrix transformation
#####1.7
- The thinking is easy
- But think further to reduce the space
- We only care about if one row or column has an zero, we don’t care about the exact location
#####1.8
- Abstract the problem!
- Again, length of strings
###2. Linked Lists
#####Creating a Linked List
First find the tail, then append the node at the end.
#####Deleting a Node
- Watch out if it’s a singly linked list or doubly linked list
- In C, C++ you’ll also have to care about memory management
#####The “Runner” Technique
Introduce a second pointer, different pointers have different pace of movement.
#####Recursive Problems
Using recursion is a way out, but that means O(n) space.
#####2.1 Remove duplicates
- LinkedListNode
- HashTable is an important thing(constant to add, find)
- Watchout if head is null
- The loop condition: (n != null)
- The code is very subtle
#####2.2 kth to last element
- Edge case!! Care about every parameter
- You never know if n.next is a null pointer or not, So check everytime you access next field
#####2.3 delete node with only access to that node
- Think harder
- Think outside the box, you may not need to actually eliminate the node in the memory, just make the linked list right after “delete”
- If n is the last node, you cannot delete it. Because you cannot make it null. Consider marking the node as dummy.
#####2.4 partition linked list around value x
- Watch out when you return something, it should not be some value instead of null
- Think about the special case where the linked list is un-balanced around x
- Be careful about pointer operations
#####2.5 linked list addition
- The recursive way is very sutle, pay attention
- Think about all the possible state of the function call
- To put code in separate method is a very clean way
- Must do it again!
#####2.6 beginning of the loop
- Find collision point is easy: runner
- But find the beginning is another story
- Need to do the math and use the property of loop
#####2.7 palindrome detect
- Only need to detect if the front half is the reverse of the second half
- Reverse order: the help of stack!
###3. Stacks and Queues
#####3.1 three stacks in one array
- Read the question carefully, seriously
- Maybe use a linked list to store all index of the stacks, take O(n) to initialize but doesn’t waste space
#####3.2 stack returns minimum in constant time
- The min changes when you push and pop
- Pay attention to memory optimization
#####3.3 set of stacks
- Need to revisit the FOLLOW-UP
#####3.4 Hanoi using stack
Base Case and Build approach:
-
Pretty easy
-
Tower 2 as a buffer
- etc
- Don’t be afraid of recursion!
#####3.5 MyQueue with two stacks
- don’t settle for naive solution, try to optimize it!
03 Jan 2014
##Chapter.1 Beginners
###1.1 Getting started
RETURN key:
no more input:
rings the bell:
backspace:
tab:
break:
which terminal:
erase one word:
erase the entire line:
Stop output:
resume
###1.2 Files and common commands
ls
-t #time order
-l #long listing
-u #when is used
-r #reverse the order
word count
compare
###1.4 Shell
####Filename shorthand
any string of characters:
range of letters or digits
any single character
turn off the special meaning
####Input-output redirection
output to file
command >filename #overwritting
command >>filename #appending
take input from a file
####Processes
no hangup:
lower priority
start command at some time
at <time> #2130 930pm
command1
command2
...
CTRL-d
##Chapter.2 The File System
###2.1 Basics of files
octal dump
od -c #character
od -b #octal numbers
od -x #hex numbers
###2.2 What’s in a file
###2.3 Directories and filenames
disc space used(listing all the files in current directory, pipe it to grep to find a specific file)
du
du -a #all files in a directory
###2.4 Permissions
3 kinds of permissions:
different people:
permission string:
- indicates it's an ordinary file
d indicates it's a directory
rws s: when the command is run, it is to be given the permissions corresponding to the file owner
directory permission:
r "read" e.g. ls
w "write" create and delete files in this dir
even if you don't have write permission to that file, you can delete it
x "search"
chmod
octal: 4 for read, 2 for write and 1 for execute
+: turns a permission on
-: turns a permission off
changing permissions on the directory doesn’t change its modification date
###2.5 Inodes
three times:
modified(written) time, used(read or executed) time, (permission) changed time
more on ls
ls -t #sorts the file according to time(default last modification)
-u #used time
-c #changed time
ls -i #the i-number in decimal
###2.6 The directory hierarchy
###2.7 Device
permission string:
c #stands for character
b #stands for block
disc free space:
the black hole:
##Chapter.3 Using the Shell
###3.1 Command line structure
tee command: save intermediate output to a file
A timer:
(sleep 300; echo Tea is ready) & # echo in 5 mins
###3.2 Metacharcters
* #any string
? #any single character
[0-9a-z] #character from range
`command` output replaces `command`
(command) run command in a sub-shell
{command} run command in current shell
$1, $2 first, second argument
'...' literally
"..." literally after $, `...` and \ interpreted
p1 && p2 run p1; if successful, run p2
p1 || p2 run p1; if unsuccessful, run p2
###3.3 Creating new commands
###3.4 Command arguments and parameters
###3.5 Program output as arguments
###3.6 Shell variables
runs command in current shell:
export: make the value of a variable accessible in sub-shells
###3.7 More on I/O redirection
direct the standard error output into a file
put the standard error on the same stream as the standard output
###3.8 Looping in shell programs
###3.9 bundle: putting it all together
# bundle: group files into distribution package
echo '# To unbundle, sh this file'
for i
do
echo "echo $i 1>&2"
echo "cat >$i <<'End of $i'"
cat $i
echo "End of $i"
done
Chapter.4 Filters
###4.1 The grep family
grep -n #print line numbers
grep -v #invert the sense of the test
grep -y #smart case matching
always a good idea to enclose grep patterns in single quotes
regular expressions:
[^0-9] #any non-digit
. #match any one character
* #closure operator- any number(including 0) of successive matches
.* #anything up to a new line
.*x #anything end with x
####grep family: fgrep, egrep
###4.2 Other filters
####sort
sort -f #case insensitive
sort -d #letters, digits and blanks only
sort -n #by numeric value
sort -r #reverse
sort -u #unique
####uniq
uniq -d #only print duplicated
uniq -u #only print unique
uniq -c #number of occurrences
####comm- compare
print 3 columns of output: lines occur only in f1, occur only in f2, occur in both
####tr- translate characters
####an idiom
###4.3 The stream editor sed
ind- stick a bat at the beginning of each line:
sed 's/^/ /' $* #Every line
sed '/./s/^/ /' $* #Non-empty line
sed '/^$/!s/^/ /' $* #Non-empty line
###4.4 The awk pattern scanning and processing language
###4.5 Good files and good filters
##Chapter.5 Shell Programming
###5.1 Customizing the cal command
Shell Built-in Variables(P136)
Shell Pattern Matching Rules(P137)
###5.2 Which command is which?
test -f filename || echo 'file 'filename' does not exist'
# is equivalent to
if test ! -f filename
then
echo 'file 'filename' does not exist'
fi
###5.3 while and until loops: watching for things
”:” is a shell built-in command that does nothing but evaluate its arguments and return “true”.
####Evaluating variables
Examine if the variable is defined or not, if not, print message:
Return value if not defined:
Return value and set var to value if not defined:
Return thing if var is defined or nothing:
###5.4 Traps: catching interrupts
###5.5 Replacing a file: overwrite
sort file -o file
# sorting the file and output to the same file
###5.6 zap: killing processes by name
###5.7 The pick command: blanks vs. arguments
About $* and $@:(P173)
###5.8 The news command: community service messages
5.9 get and put: tracking file changes
5.10 A look back
28 Dec 2013
##Modes
Table of contents of the reference manual
Index of manual
###Insert mode
:help inserting
:help replacing
###Switching modes
##Moving around
###Jump within window
‘H’igh- first line of the window
Middle of the window
‘L’ow- last line of the window
###help-motions
:help word-motions
:help cursor-motions
###Sentence wise
next sentence
previous sentence
###Paragraph wise
next paragraph
previous paragraph
###Mark
create a mark
jump to the mark
###Jump around
jump back to previous location
jump forward to next location
###Parts of the text
visual select a paragraph
a word
a quoted string
a block
flip the case
####Some help
:help object-motions
:help text-objects
:help various-motions
:help motion
###Help
List of contents of the entire user manual
###:helpgrep
:helpgrep {Beginning of a world}
Navigate
Whole list
###keywordprg
Don’t quite know it
##Editing Basics
###Swap
###Directory
Change Vim directory
Print Vim working directory
###Swap tricks
Swap 2 characters
Swap 2 words
###Time machine
Travel between time
:earlier 4m
:later 45s
:undo 5
:undolist
manual for it
:help :undolist
:help undo-redo
:help usr_32.txt
###Search
search for exact word
search for number
/\d #1 digit
/\d\+ #1 or more digits
/\d\* #0 or more digits
manual
##Multiplicity
###Fold
:set foldmethod=indent
:nnoremap <space> za
:help folding
###Buffer
open file
:e filename #open file
:b num #switch to buffer 1
buffer list
delete buffer
help
###Window
switch between windows
CTRL-w <motion key> #w means 'w'indows
cycle between windows
same file, different parts
:sp #'sp'lit window
CTRL-w s
vertical split
‘r’otate the windows
move current window to the topmost position
resize to num lines long
make current window as big as possible
make all windows ‘equal’ in height again
help manual
###Tabs
open a new tab
switch between tabs
gt #'g'o to the next 't'ab
gT #opposite direction
close a tab
open text in a new tab instead
:help tabpage
=>:tab help tabpage
tab reorder
help manual
##Scripting
###Macros
q(a-zA-Z) #record a macro
@(a-zA-Z) #replay a macro
###Scripts
List of available functions
List of features
Help manuals
:help eval
:help python-commands
:help perl-using
:help ruby-commands
##Plugins
###Syntax scheme
manuals when writing syntax scheme
:help syntax
:help usr_44.txt
:help group-name
:help pattern-overview
:help mysyntaxfile
:help new-filetype
redraw the screen
###Compiler plugin
help
:help write-compiler-plugin
:help quickfix
###Disabling plugin
vim -u NONE
vim -u your-minimal-initialization.vim
help
##Programmes Editor
###Syntax highlighting
pipe Unix shell output to Vim
cmd | vim -R -
# end dash tells Vim to read from standard input
###Bounce
jump to corresponding curly bracket, or between the start and end of a block
###Shell
access a full-fledged shell
run external filters for current text
:%!sort
# sort current content
###Jumping around
open the file(position your cursor on a file name)
go to the local definition of the variable(cursor on variable name)
go to the global declaration
next { in the first column
display all lines that contain the keyword under the cursor
help
:help object-motions
:help 29.3
:help 29.4
:help 29.5
###Browsing code
####File system
####ctags
open taglist window
jump to definition of
jump to definition of symbol under the cursor
return to previous code you were reading
jump to definition in a split window
move between matching tags
:tnext
:tprev
:tfirst
:tlast
help
##More
###Modeline
enforce pure tabs within certain file
# vim: noexpandtab
(put within the first 2 lines or last 2 lines)
###Dr.Chip’s plugins
Need to apply it
###Community
mailing list
tips
##What next
Best of Vim Tips
:help user-manual
:help new-7
###Some tricks
Cancel search highlight
###Vim/Cscope
search: CTRL-<space> s
command line search: :scscope find symbol foo
or :scs f s foo
go back: CTRL-t
####types of search
s
find all uses of symbol
g
global definition
c
all calls to a function
f
opens the filename under the cursor
####auto reindent
gg=G