Thursday, December 30, 2010

VIM

Vim Text Editor

Pros:
  • On every platform
  • 100% keyboard driven
  • Text surgery
  • powerful editing
  • Local and remote
  • Highly configurable
  • Extendable with plugins
  • Mac-friendly
  • Great Documentation
  • Long-terms skills
Cons:
  • Undesirable defaults
  • Clunky scripting Language
  • Destructive tasks are too easy
  • Inconsistent regular expressions
Started in 1976 then 1991 and now it is called VIMproved (VIM)

Main modes of VI editor
  1. Normal
  2. Insert
  3. Visual
  4. Replace
  5. Command-line
Upper case commands are super sized versions of the lower case commands

i - Inserts text starting from the cursor
I - Inserts text starting from the beginning of the line.
w - moves forward one word
3w - moves forward 3 words
b - moves backware one word
W - moves contiguous code as one word and move forward to the next meaningful word
dd - deletes the line where the cursor is located
dw - deletes the word
d2w - delete two words
G - goes to the end of the file
gg - goes to the start of the file
:w - write file (save)
:w! - write without confirmation
:q! - quit vim
:wq! - write and quit
:e filename - opens a file to edit
code folding command starts with a z
 
cw - change word
3cw - change three words
:h - help:bd - buffer delete

yy - yank line (copy)
Y - yank line (copy)
p - paste below cursor
P - paste above cursor
a - append text after cursor
6l - Forward six letters
2j - Down two lines
fN - Jump forward to the first 'N'
3fN - Jump forward to third 'N'
n- Next 

u - undo
Ctrl+R - redo

vim -N abcd.py (-N compatible with the latest version only)

Vim comes with 500 different syntax highlights.

:syntax enable
:set syntax=apache

:set hidden
:set syntax=python
:set syntax=cpp

:w !sudo tee %

:set incsearch
:set hlsearch


Search and replace

:%s/search/replace/gc

  % search the current buffer
  g - search for all occurance
  c - ask for confirmation on each match

e.g :%s/2.\2\.8/2.2.9/gc - this will replace all 2.2.8 occurances to 2.2.9 and will confirmation all occurances.

:set number
12G - go to line 12

:cd path
:pwd
:e . - to find out the files in the directory


V - visual mode

:b name - Switch to buffer (try TAB and arrows as well)
:bp - Previous buffer

:set autochdir - Automatically use the current file's directory as the working directory


Check out the vim-rooter plugin.

:set wildmode=list:longest - Activate TAB auto-completion for file paths.

Directory listing.

i - thin, long, wide or tree listings
s - sort on name, time or file size
r - Reverse sort order
gh - Hide or unhide dot files
- Open the file or directory
x - View file with associated application
d - Make directory
D - delete the file or directory
R - Rename the directory or file
- <-- Go up a directory


Window Management

Ctrl+w s - split window horizontally
Ctrl+w v - split vertically
Ctrl+w j - Move focus down
Ctrl+w k - Move focus up
Ctrl+w h - Move focus left
Ctrl+w l - Move focus right

Ctrl+w J - Move buffer up one window
Ctrl+w K - Move buffer down one window
Ctrl+w H - Move buffer left one window
Ctrl+w L - Move buffer right one window

Ctrl+w c - Close window
Ctrl+w o - Close all but current
Ctrl+w w - Cycle focus
Ctrl+w p - Focus previous window

Friday, October 15, 2010

Puzzle

Came along an interesting puzzle so wanted to share.

Write a function that adds two numbers.  You should not use + or any arithmetic operators.

Lets have two number which are fairly large and try to add those two numbers but try a different approach.

987+789 = 1776

Lets just think backwards

   987
+ 789
--------
   666

Carry : 1110.  This is calculated when the first carry is the first digit and the last carry is the last.

Add 1110 and 666 which gives you the ultimate result.

Now lets implement it in source (I am using python)

>>> def sum_without_arithmetic(a,b):
...     if b==0: 
...         return a
...     sum = a^b
...     carry = (a&b)<<1
...     return sum_without_arithmetic(sum, carry)
...
>>> sum_without_arithmetic(987,789)
1776

Thursday, October 14, 2010

More Interview Questions

Networking:

Explain what happens, step by step, after you type a URL into a browser.  Use as much details as possible.

1. Browser contacts the DNS server to find the IP address of the URL.
2. DNS server returns the IP address of the site.
3. Browser opens a TCP connection to the web server at port 80.
4. Browser fetches the html code of the page requested.
5. Browser renders the HTML in the display window.
6. Browser terminates the connection when window is closed.


Database:

1. What are different types of join?  Please explain how they differ and why certain types are better in certain conditions.

JOIN is used to combine the results of two tables.  To perform a join, each of the tables must have at least one field which will be used to find matching records from the other table.  The join type defines which records will go into the result set.


1. INNER JOIN:

Result set will contain only those data where the criteria match.

2. OUTER JOIN:

Outer join will always contain the results of INNER JOIN, however it can contain some records that have no matching record in other table.  OUTER JOINs are divided into following subtypes

2.1 LEFT OUTER JOIN or simply LEFT JOIN:

The result set will contain all the records from the left table.  If no matching records were found in the right table, then its fields will contain NULL values.

2.2 RIGHT OUTER JOIN or simply RIGHT JOIN:

Opposite of the LEFT JOIN.  It will contain all the records from the right table, and missing fields from the left table will contain NULL.

If we have two tables A and B, then we can say that statement A LEFT JOIN B is equivalent to statement B RIGHT JOIN A.

2.3 FULL OUTER JOIN

FULL OUTER JOIN combines the results of LEFT and RIGHT joins.  All records from both the tables will be part of the result set, whether matching records exists in other table or not.  If no matching record was found, then the corresponding result fields will have a NULL value.

C++

Compare and contrast Hash Table vs. STL map.

Hash Table:

In a hash table, a value is stored by applying hash function on a key.  Thus, values are not stored in a hash table in sorted order.  Additionally, since hash tables use the key to find the index that will store the value, an insert/lookup can be done in amortised O(1) time (assuming a few collisions in a hash table).  One must also handle potential collisions in a hash table.

STL Map

In STL Map, insertion of key/value pair is in sorted order of key.  It uses a treee to store values, which is why an O(log n) insert/lookup is required.  There is also no need to handle collisions.  An STL map works well for things like:
  • find min element
  • find max element
  • print elements in a sorted order
  • find the exact element or if the element is not found, find the next smallest number.
Implementation of a Hash Table

  1. A good hash function is required (e.g. operation % prime number) to ensure that the hash values are uni-formally distributed.
  2. A collision resolving method is also needed: chaining (good for dense table entries), probing (good for sparse table entries), etc.
  3. Implement methods to dynamically increase or decrease the hash table size on a given criterion.  For example, when the [number of elements] by [table size] ratio is greater than the fixed threshold, increase the hash table size by creating a new hash table and transfer the entries from the old table to the new table by computing the index using new hash function.
What can be used instead of a hash table, if the number of inputs is small?

You can use an STL map.  Although this takes O(log n) time, since the number of inputs is small, this time is negligible.

Wednesday, May 5, 2010

C++ style puzzles

Some interesting puzzles that can be asked in an interview.

1. Given a vector v, what is the difference between the lines marked A and B?

void f(vector& v) {
v[0]; // A
v.at(0); // B
}

If v is not empty then there is no difference between lines A and B. If v is empty, then line B is
guaranteed to throw a std::out_of_range exception, but there's no telling what line A might do.
There are two ways to access contained elements within a vector. The first, vector::at, is required to perform bounds-checking to ensure that the vector actually contains the requested element. It doesn't make sense to ask for, say, the 100th element in a vector that contains only 10 elements at the moment, and if you try to do such a thing, at will protest by throwing a std::out_of_range hissy fit (also known as an exception).

Saturday, January 30, 2010

dil to bacha hai ji -- ISHKIYA guitar chords

I have been learning to play guitar for some years now.

Here is my attempt to decode the chords of a new song.

“Dil toh bacha hai ji” from the movie Ishqiya is a good

slow song. Reminds me of the old Rajkapoor movie songs.

Rahat Fateh Ali Khan’s voice has made this song even

better.

Now, to play this song on guitar you show know to play the

Bar Chords.

Strumming pattern: |DD - DD -|DD – DD -|

F#m

Aisi ulji nazar unse hatt ti nahi

Bm

Daant se reshmi dor katt ti nahi

F#m
Umar kab ki baras ke safaid ho gayi

Bm
Kaari badari jawani ki chatt ti nahi

F#m        Bm

Walla ye dhadkan bhadne lagi hai

F#m        Bm
Chehre ki rangat udne lagi hai

F#m        Bm
Darr lagta hai tanha sone mein ji
F#m         Bm        C#m

Dil to bachcha hai ji

F#m        Bm        C#m
Dil to bachcha hai ji

F#m        Bm        C#m
Thoda kaccha hai ji

F#m        Bm        C#m
Haan dil to baccha hai ji

F#m
Aisi ulji nazar unse hatt ti nahi

Bm

Daant se reshmi dor katt ti nahi

F#m

Umar kab ki baras ke safaid ho gayi

Bm

Kaari badari jawani ki chatt ti nahi

F#m        Bm        C#m
Ra ra ra .. x 2

F#m        F#
Kisko pataa tha pehlu mein rakha

Bm
Dil aisa baaji bhi hoga

F#m        F#
Hum to hamesha samajhte the koi

Bm
Hum jaisa haaji hi hoga

F#m        Bm
Hai zor karein, kitna shor karein

F#m        Bm
Bewaja baatein pe ainwe gaur karein

Bm
Dilsa koi kameena nahi

F#m        Bm
Koi to rokey, koi to tokey

F#m        Bm
Iss umar mein ab khaogey dhokhe

F#m        Bm
Darr lagta hai ishq karne mein ji

F#m        Bm        C#m
Dil toh bachcha hai ji

F#m        Bm        C#m
Dil toh bachcha hai ji

F#m         Bm         C#m

Thoda kaccha hai ji

F#m          Bm         C#m

Haan dil to baccha hai ji

F#m         F#

Aisi udhaasi baithi hai dil pe

Bm

Hassne se ghabra rahe hain

F#m         F#

Saari jawani katra ke kaati

Bm

Piri mein takra gaye hain

F#m         Bm

Dil dhadakta hai to aise lagta hai who

F#m         Bm
Aa raha hai yahin dekhta hi na woh

F#m         Bm

Prem ki maarein kataar re

F#m         Bm

Taubah ye lamhe katt te nahi kyun

F#m         Bm

Aankhein se meri hatt te nahi kyun

F#m         Bm

Darr lagta hai mujhse kehne mein ji

F#m         Bm         C#m

Dil toh bachcha hai ji

F#m         Bm         C#m

Dil toh bachcha hai ji

F#m         Bm         C#m

Thoda kaccha hai ji

F#m         Bm         C#m

Haan dil toh baccha hai ji