Friday, May 10, 2019

Big O Notation With Sample codes

Table of Summary:

enter image description here

Examples

O(1) - Constant Time Examples:
  • Algorithm 1:
Algorithm 1 prints hello once and it doesn't depend on n, so it will always run in constant time, so it is O(1).
print "hello";
  • Algorithm 2:
Algorithm 2 prints hello 3 times, however it does not depend on an input size. Even as n grows, this algorithm will always only print hello 3 times. That being said 3, is a constant, so this algorithm is also O(1).
print "hello";
print "hello";
print "hello";
O(log(n)) - Logarithmic Examples:
  • Algorithm 3 - This acts like "log_2"
Algorithm 3 demonstrates an algorithm that runs in log_2(n). Notice the post operation of the for loop multiples the current value of i by 2, so i goes from 1 to 2 to 4 to 8 to 16 to 32 ...
for(int i = 1; i <= n; i = i * 2)
  print "hello";
  • Algorithm 4 - This acts like "log_3"
Algorithm 4 demonstrates log_3. Notice i goes from 1 to 3 to 9 to 27...
for(int i = 1; i <= n; i = i * 3)
  print "hello";
  • Algorithm 5 - This acts like "log_1.02"
Algorithm 5 is important, as it helps show that as long as the number is greater than 1 and the result is repeatedly multiplied against itself, that you are looking at a logarithmic algorithm.
for(double i = 1; i < n; i = i * 1.02)
  print "hello";
O(n) - Linear Time Examples:
  • Algorithm 6
This algorithm is simple, which prints hello n times.
for(int i = 0; i < n; i++)
  print "hello";
  • Algorithm 7
This algorithm shows a variation, where it will print hello n/2 times. n/2 = 1/2 * n. We ignore the 1/2 constant and see that this algorithm is O(n).
for(int i = 0; i < n; i = i + 2)
  print "hello";
O(n*log(n)) - nlog(n) Examples:
  • Algorithm 8
Think of this as a combination of O(log(n)) and O(n). The nesting of the for loops help us obtain the O(n*log(n))
for(int i = 0; i < n; i++)
  for(int j = 1; j < n; j = j * 2)
    print "hello";
  • Algorithm 9
Algorithm 9 is like algorithm 8, but each of the loops has allowed variations, which still result in the final result being O(n*log(n))
for(int i = 0; i < n; i = i + 2)
  for(int j = 1; j < n; j = j * 3)
    print "hello";
O(n^2) - n squared Examples:
  • Algorithm 10
O(n^2) is obtained easily by nesting standard for loops.
for(int i = 0; i < n; i++)
  for(int j = 0; j < n; j++)
    print "hello";
  • Algorithm 11
Like algorithm 10, but with some variations.
for(int i = 0; i < n; i++)
  for(int j = 0; j < n; j = j + 2)
    print "hello";
O(n^3) - n cubed Examples:
  • Algorithm 12
This is like algorithm 10, but with 3 loops instead of 2.
for(int i = 0; i < n; i++)
  for(int j = 0; j < n; j++)
    for(int k = 0; k < n; k++)
      print "hello";
  • Algorithm 13
Like algorithm 12, but with some variations that still yield O(n^3).
for(int i = 0; i < n; i++)
  for(int j = 0; j < n + 5; j = j + 2)
    for(int k = 0; k < n; k = k + 3)
      print "hello";

Big O Notation

https://stackoverflow.com/questions/2307283/what-does-olog-n-mean-exactly
O(log(N))
O(log N) basically means time goes up linearly while the n goes up exponentially. So if it takes 1second to compute 10 elements, it will take 2 seconds to compute 100 elements, 3 seconds to compute 1000 elements, and so on.
​It is O(log n) when we do divide and conquer type of algorithms e.g binary search. Another example is quick sort where each time we divide the array into two parts and each time it takes O(N) time to find a pivot element. Hence it N O(log N)
Phone Book example:
  • O(1) (best case): Given the page that a business's name is on and the business name, find the phone number.
  • O(1) (average case): Given the page that a person's name is on and their name, find the phone number.
  • O(log n): Given a person's name, find the phone number by picking a random point about halfway through the part of the book you haven't searched yet, then checking to see whether the person's name is at that point. Then repeat the process about halfway through the part of the book where the person's name lies. (This is a binary search for a person's name.)
  • O(n): Find all people whose phone numbers contain the digit "5".
  • O(n): Given a phone number, find the person or business with that number.
  • O(n log n): There was a mix-up at the printer's office, and our phone book had all its pages inserted in a random order. Fix the ordering so that it's correct by looking at the first name on each page and then putting that page in the appropriate spot in a new, empty phone book.
For the below examples, we're now at the printer's office. Phone books are waiting to be mailed to each resident or business, and there's a sticker on each phone book identifying where it should be mailed to. Every person or business gets one phone book.

Horse Loop:
Ok let's try and fully understand what a logarithm actually is.
Imagine we have a rope and we have tied it to a horse. If the rope is directly tied to the horse, the force the horse would need to pull away (say, from a man) is directly 1.
Now imagine the rope is looped round a pole. The horse to get away will now have to pull many times harder. The amount of times will depend on the roughness of the rope and the size of the pole, but let's assume it will multiply one's strength by 10 (when the rope makes a complete turn).
Now if the rope is looped once, the horse will need to pull 10 times harder. If the human decides to make it really difficult for the horse, he may loop the rope again round a pole, increasing it's strength by an additional 10 times. A third loop will again increase the strength by a further 10 times.
enter image description here
We can see that for each loop, the value increases by 10. The number of turns required to get any number is called the logarithm of the number i.e. we need 3 posts to multiple your strength by 1000 times, 6 posts to multiply your strength by 1,000,000.
3 is the logarithm of 1,000, and 6 is the logarithm of 1,000,000 (base 10).

Thursday, August 2, 2018

Expect script

Link: https://likegeeks.com/expect-command/

Thursday, March 29, 2018

Important GIT tricks


  1. To view the diff of the files using vimdiff, 
    1. Open ~/.gitconfig and append the following lines
      [diff]
        external = git_diff_wrapper
NOTE: you can also do it, by issuing "git config --global diff.tool vimdiff", but I have not tried.
References: https://technotales.wordpress.com/2009/05/17/git-diff-with-vimdiff/
https://stackoverflow.com/questions/3713765/viewing-all-git-diffs-with-vimdif

2. To ignore the cscope files showing from "git status [-s]", do the following.
- go to the working directory
- and add cscope file(s) in .gitignore file

Thursday, August 3, 2017

Logical Reasoning - Interview questions

There are 8 chemicals out of which one is poisoned. You have three lab rats to test them on. Each chemical takes 10 mins to work. How will you find out the poisoned chemical in the least possible time?



Thursday, August 20, 2015

Grep with function name (with different colors) recursively

#!/bin/csh

#

# Maximum can be any parameters (if user has given '*' as files)
#
if ( $#argv < 1 ) then
    echo "\n[USAGE]: $0 <pattern> <files>\n";
    exit
endif

#awk -v re=$1 '/^[[:alpha:]]/{f=FNR"-"$0} $0~re{printf "%s\n%d:%s\n--\n",f,FNR,$0; f="" }' $2
#awk -v re='CANMIC_MX_LOCK' '/^[[:alpha:]]/{f=FNR"-"$0} $0~re{printf "\033[1;31m%s\n\033[34m%d:\033[0m%s\n--\n",f,FNR,$0; f="" "\033[0m" }' *
#awk -v re=$1 '/^[[:alpha:]]/{f=FNR"-"$0} $0~re{printf "\033[1;31m%s\n\033[34m%d:\033[0m%s\n--\n",f,FNR,$0; f="" "\033[0m" }' $2
    foreach i (`find . -name "*" -type f -print`)
    awk -v re=$1 '/^[[:alpha:]]/{f=FNR"-"$0} $0~re{printf "\033[1;32m[%15s]\t\033[1;31m%-64s\t\033[34m%s\n--\033[0m\n",FILENAME,f,$0; f="" }' $i
    end

Output:
[./canmic/canmic_err.c] 53-canmic_state_set(int newstate, int *oldstate)                                CANMIC_MX_LOCK();
--
[./canmic/canmic_err.c] 77-canmic_stateful_write(char *blkdata, size_t blksize, int blknmbr)            CANMIC_MX_LOCK();
--
[./canmic/canmic_err.c] 93-canmic_stateful_dirty(scnMsg_t *scnmsg)                                      CANMIC_MX_LOCK();
--
[./canmic/canmic_err1.h]                                                                                #define CANMIC_MX_LOCK()        do {    \
--
[./canmic/canmic_err.h]                                                                         #define CANMIC_MX_LOCK()        do {    \

Tuesday, January 6, 2015

Samsung Secret codes to test

*#0*#  => To test all the available features in the Samsung phone
*#self# (or *#7353#) ==> To test the sensors and camera

References:
http://www.androidcentral.com/how-uncover-and-use-hidden-service-menu-galaxy-s3

Sunday, October 19, 2014

Hello World Assembly Programming

; Macros used in NASM assembler
%define SYS_EXIT    1
%define SYS_READ    3
%define SYS_WRITE   4
%define STDOUT      1
%define STATUS      0
%define SYS_INTR    0x80

section .data
    hello: db 'Hello World!', 10 ;NOTE This 10 is for new line
    ;hello: db 'Hello World!'
    helloLen: equ $-hello           ;This will have the length of helloLen
                                    ; Note unlike "hello", helloLen is not an address; but value

section .text
    global _start

_start:
        ;Printing Hello World String.
        mov eax, SYS_WRITE
        mov ebx, STDOUT
        mov ecx, hello
        mov edx, helloLen
        int SYS_INTR

        ;Exit from main program
        mov eax, 1
        mov ebx, 0
        int SYS_INTR
        ret                 ; This will not get executed at all

Command to Assemble:

nasm -f elf hello.asm

Command to Link:
ld -s -o hello hello.o -m elf_i386 (Note elf_i386 denotes that this needs to be linked for 32 bit application)

Friday, October 10, 2014

DAS, NAS, SAN (FC & ISCSI & FCOE)

LINKS:
http://www.stonefly.com/resources/watch.asp?vid=FCoE-vs-iSCSI,-What%27s-The-Better-Choice-For-You#video


DAS:

SERVER-SERVER: 
PHYSICAL: ETHERNET
PROTOCOL: TCPIP
SERVER-STORAGE:
PHYSICAL: SCSI (SATA CARDS)
PROTOCOL: SCSI
CONS:
Free Storage in one server could not be shared with other


NAS:

SERVER-SERVER: 
PHYSICAL: ETHERNET
PROTOCOL: TCPIP
SERVER-STORAGE:
PHYSICAL: ETHERNET
PROTOCOL: TCPIP
PROS:
- Centralized Storage
- Sharable
- Data can be accessed as \\12.11.1.2\file_name (means, the server or host knows the storage is not Local)
CONS:
- File Level Transfer, hence slower
- Did support all applications (like mail exchange, database so on)
But now it supports all the applications



SAN (with FC):

SERVER-SERVER: 
PHYSICAL: ETHERNET
PROTOCOL: TCPIP
SERVER-STORAGE:
PHYSICAL: Fiber Channel
PROTOCOL: Fiber Channel
PROS:
- Centralized Storage
- Block Level Transfer
- Data can be accessed as D:\ (which means, the server assumes the storage is local BUT IT IS NOT)
CONS:
- High Cost
- New deployment needed server to have HBA (new PCI card)



SAN (with iSCSI):

SERVER-SERVER: 
PHYSICAL: ETHERNET
PROTOCOL: TCPIP
SERVER-STORAGE:
PHYSICAL: Ethernet
PROTOCOL: ISCSI (NOT TCPIP; and hence supports BLOCK_TRANSFER)
PROS:
- Centralized Storage
- Block Level Transfer
- No need for HBA
CONS:
- NO CONS IDENTIFIED AS OF NOW (IT IS TAKING OVER FC MARKET)



SAN (with FCOE):

SERVER-SERVER: 
PHYSICAL: ETHERNET
PROTOCOL: TCPIP
SERVER-STORAGE:
PHYSICAL: Ethernet
PROTOCOL: Fiber Channel (NOT TCPIP or ISCSI)
PROS:
- Centralized Storage
- Block Level Transfer
CONS:
- FCOE HBA Cards are needed for servers



Monday, May 12, 2014

Screen Command in Linux

screen:
=========
Ctrl-a c Create new window (shell)
Ctrl-a k Kill the current window (could be ctrl+a <shift k>)
Ctrl-a w List all windows (the current window is marked with "*")
Ctrl-a 0-9 Go to a window numbered 0-9
Ctrl-a n Go to the next window
Ctrl-a Ctrl-a Toggle between the current and previous window
Ctrl-a [ Start copy mode
Ctrl-a ] Paste copied text
Ctrl-a ? Help (display a list of commands)
Ctrl-a Ctrl-\ Quit screen
Ctrl-a D (Shift-d) Power detach and logout
Ctrl-a d Detach but keep shell window open
Ctrl-a Esc TO SCROLL THROUGH THE PREVIOUS BUFFERS (V V USEFUL)

References:
http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/
http://aperiodic.net/screen/quick_reference