Saturday 26 December 2020

C++ STL and data structures - part 1

Please share it with your friends who are preparing for interview , and comment down your suggestions , or any mistake in the post. 

code snippets are hosted on ideone.com. you can run code there as well.


Before moving ahead, I would highly recommend going through following video from google interview preparation, to understand about how coding interviews are conducted. Remember coding interviews are also about understanding your thought process. So, whatever you think during a coding interview just speak it out. If you found a raw solution like brute force or O(n^2) even start with that. But keep it going.

PS: One my friend got straight asked this question during interview :-) 




1) O(1) , O(longn) , O(n), O(nlogn), O(n^2):

These are ways to measure space and time complexity. O(1) is a constant time so it takes constant time to process irrepective of input for example :

int square( int n )

{

    return n*n;

}

O(n) is also called linear time for example searching an element in unsorted array

O(n^2) is quadratic time , a bubble sort ( sorting using double for loops ) is quadratic time.

Binary search takes O(logn) time , as input gets half at each itration.

And most of good sorting algorithams ( Quick sort , merge sort ) have best case complexity of O(nlogn).

I have used this website to create some chart. remeber that importance of big O notation is with large input size , but to just visualise I have kept input size smaller.

remember in big O notations we drop constant times.

Ref :

< https://developerinsider.co/big-o-notation-explained-with-examples/ >

https://developerinsider.co/big-o-notation-explained-with-examples/ >

fooplot chart >






2) Linked list:

https://www.geeksforgeeks.org/linked-list-set-1-introduction/ >

https://www.geeksforgeeks.org/linked-list-set-2-inserting-a-node/ > 

https://www.geeksforgeeks.org/delete-a-linked-list-node-at-a-given-position/ >

https://www.geeksforgeeks.org/linked-list-set-3-deleting-node/ >

https://www.geeksforgeeks.org/check-if-a-linked-list-is-circular-linked-list/ >

https://www.geeksforgeeks.org/count-duplicates-in-a-given-linked-list/ >

https://www.geeksforgeeks.org/remove-duplicates-from-an-unsorted-linked-list/ >

https://www.geeksforgeeks.org/remove-duplicates-from-a-sorted-linked-list/ >


3) Hashtables in C++ :

remember correct selection of your data structure is necessary as each have different space and time complexity.

1. Do you want to store only keys ?


a. only uniques are allowed ?


set < type > var_name ; => if ordering is needed

unordered_set < type > var_name; => if ordering is not needed


b. duplicate keys allowed ?


multiset < type > var_name ; 

unordered_multiset < type > var_name;


2. Do you want to store keys and as well as values ?


a. only uniques are allowed ?


map < type > var_name ; => if ordering is needed

unordered_map < type > var_name; => if ordering is not needed


b. duplicate keys allowed ?


multimap < type > var_name ; 

unordered_multimap < type > var_name;


4) Unordered_set in C++ :

difference between set and map : < https://www.geeksforgeeks.org/set-vs-map-c-stl/ >


5)  Vectors in C++ :

Vectors are widely used as they are exactly dynamic array. So for any operation on vector you can think of array.

Traversing vector is easy , but inserting/deleting element at front or in middle is difficult.

Doing push_back is O(1).

It is contagious memory.

vector tutorial : < https://www.geeksforgeeks.org/passing-vector-function-cpp/ >

vector passing  : < https://www.geeksforgeeks.org/vector-in-cpp-stl/ >

difference between pointer and referance : < https://www.geeksforgeeks.org/pointers-vs-references-cpp/ >


6) List in C++ :

The list in C++ is doubly link list. you can add at front as well as back. and you can travese from front as well as back.

As list is doubly link list, 2 pointers are stored , if you need signly linked list use forward_list which was introduced in C++ 11.

Travesing list takes time.But insert/delete is easy once element is found.

push_back and push_front is O(1).

It can be non contagious memory.

sort() for sorting and reverse() to reverse the list.


7)  Queue in C++ :


STL Queue provide addition at back ( push ) and deletion at front ( pop ).

With deque ( double ended queue ) you can do push and pop at both ends.

Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in non increasing order (hence we can see that each element of the queue has a priority {fixed order}).


8) Stack in C++ :


STL stack provide addition at back ( push ) and deletion at back ( pop ).
It's a LIFO container.
Besides push() and pop0 there is top() as well. which provides top() element of stack.

9) Sorting algorithm :


Besides in-built sorting method provided with containters in STL , there is also "sort" algoritham as part of #include <algorithm >




10 ) binary_search :

even though most of the stl containers provides find() method , there is also generic binary_search algo as part of #include <algorithm >.


Bonus :

Please share it with your friends who are preparing for interview , and comment down your suggestions , or any mistake in the post. 

code snippets are hosted on ideone.com. you can run code there as well.



















Tuesday 15 December 2020

C interview questions - part 4

 1) write a recursive function to print number from -5 to 5.


2) write a recursive function to find factorial of number.


3) string is palindrom or not

*< https://www.geeksforgeeks.org/c-program-check-given-string-palindrome/ >


4) write C program for LALR parsing 


5) explain arithmatic shift vs logical shift

< https://stackoverflow.com/questions/7622/are-the-shift-operators-arithmetic-or-logical-in-c >

note : shift operators in C are logical shift operators. you loose bits once you rotate


6) write a C program to rotate bits in number / arithmatic rotate.

< https://www.geeksforgeeks.org/rotate-bits-of-an-integer/ >


7) count number of bit needed for number 

< https://www.geeksforgeeks.org/count-total-bits-number/ >

number of bit = log2(n)+1


8) swap all bits in a number

you can construct your own program using above information.

or you can use this one < https://www.geeksforgeeks.org/swap-bits-in-a-given-number/ >


9) XOR , AND , OR truth tables


10) find repetative number in given array 

< https://www.geeksforgeeks.org/find-the-two-repeating-elements-in-a-given-array/ >


11) write a C program to find a number in given sorted array using binary search.

< https://www.geeksforgeeks.org/binary-search/ >


12) write a program to find endianness of given system.

< https://www.geeksforgeeks.org/little-and-big-endian-mystery/ >

https://stackoverflow.com/questions/12791864/c-program-to-check-little-vs-big-endian >


13) Using semphore in a C program :

https://www.geeksforgeeks.org/use-posix-semaphores-c/ >


14) Using mutex lock in C program :

 < https://www.geeksforgeeks.org/mutex-lock-for-linux-thread-synchronization/ >


15) How to pass arguments from command line in C :

< https://www.geeksforgeeks.org/command-line-arguments-in-c-cpp/ >


16) what are expressions optional in for loop ?

All the three expressions inside the for loop are optional, but the two semicolons must always be present.

  • We can omit the expression1 if the initialization is done outside the for loop.
  • If the expression2 is omitted then the condition is always true, leading to the creation of an infinite loop - a loop which never stops executing. To avoid infinite loops you should include a break or return statement within in the loop body. We discuss the break and return statements in detail in upcoming chapters.
  • We can omit the expression3 if the update expression is present inside the body of the for loop.< The for loop in C - C Programming Tutorial - OverIQ.com >



Monday 14 December 2020

Electronics/Embedded/Electrical questions - part 1

Please share it with your friends who are preparing for interview , and comment down your suggestions , or any mistake in the post. 

code snippets are hosted on ideone.com. you can run code there as well.


Any good embedded engineer should have following skills besides programming skills:

1) understanding of electronics hardware like understanding circuit diagrams, resister, capacitors, basic low pass, high pass filters, band pass filter, isolation techniques etc.

2) should know how to use oscilloscope, logic analyzer, function generator, multimeters etc.

3) should know how to use hardware debuggers like JTAG and features like Trace, data breakpoint, memory window, register window etc.

4) understanding of peripherals like IOs, ADC, DAC, PWM , UART , SPI,  I2C.  

5) basic understanding of architecture like ARM , MIPS , PIC , AVR , MSP430.

-----------------------------------------------------------------------------------------------------------

In this post I will list down some of the articles, that you must go through before any embedded inteview.

I would highly recommend 2 websites for embedded electronics knowledge :

1) https://learn.sparkfun.com/

2) https://www.engineersgarage.com/


1) Understanding UART :

< https://learn.sparkfun.com/tutorials/serial-communication/all >


2) Understanding I2C :

< https://learn.sparkfun.com/tutorials/i2c/all >

I2C Speed:

Original : 100 Kbps

Fast mode : 400 kbps

Fast mode plus : 1 Mbps

High speed mode : 3.4 Mbps

Addressing:

7-bit addressing => 112 number of devices ( because there are some reserved addresses )

10-bit addressing => 


3) Understanding SPI :

< https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi/all


4) Difference between RS485 vs RS232 :

https://www.electronicdesign.com/technologies/communications/article/21800966/whats-the-difference-between-the-rs232-and-rs485-serial-interfaces >


5) What is Modbus protocol :

https://www.bb-elec.com/Learning-Center/All-White-Papers/Modbus/The-Answer-to-the-14-Most-Frequently-Asked-Modbus.aspx >


6) SAR ADC :

Remember for ADC sampling rate , resolution is 2 important parameters.

https://learn.sparkfun.com/tutorials/analog-to-digital-conversion/relating-adc-value-to-voltage >



7) SAR vs Sigma Delta ADC :


8) Write a program to implement software UART , I2C or SPI ( mvxpiam )

Software protocol implementation like UARTs, I2Cs , SPIs are needed when there is no physical hardware present on chip for UART , I2C , SPI.

This is very interesting exercise to do for any embedded engineer at it involves bit banging.

Bit banging is slang for any method of data transmission that employs software as a substitute for dedicated hardware to generate transmitted signals or process received signals.

< src : https://en.wikipedia.org/wiki/Bit_banging >

Try to think different scenarios like :

Blocking call vs non blocking call.

Single byte buffer vs Multi byte buffer etc.


9) What is bit banding region in memory ?

Normally memory is "byte" addressable i.e the minimum unit you can access is byte. But bit banding region is bit addressable i.e you can address individual bits. This region is widely used for user defined flags and boolean variables.


10) what is JTAG debugger ?

Sometimes explaining JTAG to programmers like web developers or desktop application developers becomes difficult. Most of the embedded developement is cross development i.e you are developing software on one architecture ( i.e x86_64 intel/amd )  which is meant to run on a different architecture. ( i.e ARM, MIPS , AVR, PIC, MSP430 etc ).

So to debug your code on that architecture  ( i.e ARM, MIPS , AVR, PIC, MSP430 etc ) you need a debuger which will connect you to that processor.

"JTAG is a common hardware interface that provides your computer with a way to communicate directly with the chips on a board. It was originally developed by a consortium, the Joint (European) Test Access Group, in the mid-80s to address the increasing difficulty of testing printed circuit boards (PCBs). JTAG has been in widespread use ever since it was included in the Intel 80486 processor in 1990 and codified as IEEE 1491 that same year. Today JTAG is used for debugging, programming and testing on virtually ALL embedded devices."

< src : https://blog.senr.io/blog/jtag-explained >


11) what JTAG features a programmer should know ?

JTAG provides most of the features which you find in other IDEs like visual studio C# or Eclipse JAVA.

Some the features are :

disassembly flow

register window

data memory window

program memory window

Instruction trace.

data breakpoint

conditional breakpoint

see this video :




11) what is an "interrupt" ?

Stands for "Interrupt Service Routine." An ISR (also called an interrupt handler) is a software process invoked by an interrupt request from a hardware device. It handles the request and sends it to the CPU, interrupting the active process. When the ISR is complete, the process is resumed.

< src : https://techterms.com/definition/isr >

12) Single vector vs multi vector interrupts 

With single vector mode, all interrupts go to the same interrupt handler.

With multi vector mode, each interrupt has its own vector.

In single vector mode, you software has to determine which interrupt source is active in order to start the appropriate processing.

In multi vector mode, once each interrupt handler is dedicated to a specific interrupt source so when in the ISR you know what you have to process.

< src microchip forum : https://www.microchip.com/forums/m414867.aspx >


13) so what is "vector" in vectored inturrupts ?

let say you are in code some where at 0x1000 and you what to go to 0x2000. now you have to use some branching or jump instruction for this. basically this is done by software.

But in case of vectored interrupts no brach or jump intructions are stored , instead vector addresses are stored for ISR and hardware ( i.e processor ) moves the control at vector address.

I would highly recommend to go through this guide :

https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/beginner-guide-on-interrupt-latency-and-interrupt-latency-of-the-arm-cortex-m-processors >


14) What is "nested" vectored inturrupts ?

Most of the microcontrollers provides some kind of priorities to interrupts. In case of nested inturrupts as the name suggests when a high priority inturrepts arrives a when low priority inturrupt is being served , then processer will shift to high priority inturrupt and when it is done , control is resumed to low priority one.

This is called nesting of inturrupts. And hence we arrive to the ARM term Nested vectored inturrupt controller.


Please share it with your friends who are preparing for interview , and comment down your suggestions , or any mistake in the post. 

code snippets are hosted on ideone.com. you can run code there as well.










Real time OS and Computer architecture questions - part 1

In this post we will try to go through some of the interview questions asked during qualcomm interview :

https://www.geeksforgeeks.org/qualcomm-audio-interview-experience-for-software-engineer/ > 


1) Difference between semaphore , mutex , and spinlock :

mutex =>

This is lock and unlock mechanism.

i.e one thread will lock and same thread can unlock that.

other threads can wait for lock to be released.

This is used for synchronization.


semaphore => 

This can be counting or binary semaphore.

Binary semaphore can be used as mutex.

But mutex cannot be used as binary semaphore.

Other thread will wait for signal.

This is classing producer consumer problem.

Consumers will wait for signal of producer.

This is used for signaling mechanism.


spinlock => 

spinlock is also a locking mechanism like mutex.

Major difference is that in case of simple mutex, it loses quantum of the

processor when some thread is waiting for some lock to be released, i.e that behave similar

to having a sleep() and processor moves to next thread, and come back after a while to see

if that lock is released or not.

spinlock behaves like a checking lock in a while(1) i.e it keeps the quantum of

processor and waits there until that lock is released. spinlock is useful in case of interrupts as 

we can loose the current context for interrupt i.e we can allow preemption of interrupt


2) Provide some crude implementation of a mutex in C :

well I was not able to find some good implementation of mutex in C , But write in comment section if you found any.


3) Provide some crude implementation of a semaphore in C :

https://www.geeksforgeeks.org/producer-consumer-problem-using-semaphores-set-1/ >

static int semphore;

void signal_or_put( )

{

    semphore++;

}

void wait_or_get()

{

    while( semphore <=0 );

        semphore--;

}


4) what is difference between process and thread :

https://www.geeksforgeeks.org/difference-between-process-and-thread/ >

< https://stackoverflow.com/questions/200469/what-is-the-difference-between-a-process-and-a-thread >

https://stackoverflow.com/questions/41632073/do-threads-share-local-variables >

Threads are small executable section of task; a task can have multiple threads.

The process can have the following states like new, ready, running, waiting, terminated, suspended.

A thread has 3 states: running, ready, and blocked.

Threads shares resources of the process like text, data, IO, process id of parent, and the heap.

However, threads have their own stack frame.

Like process ids threads have thread ids.


5) Memory layout of a C program :

https://www.geeksforgeeks.org/memory-layout-of-c-program/ >


6) Inter process communications :

https://www.geeksforgeeks.org/inter-process-communication-ipc/ >

pipe vs message queue :

https://www.tutorialspoint.com/difference-between-pipes-and-message-queues >

pipe vs named pip :

https://www.quora.com/What-is-the-difference-between-named-and-unnamed-pipes-in-Linux-programming >


7) What happens when 2 same priority interrupt occurs :

< https://www.quora.com/What-happens-when-two-interrupts-with-the-same-priority-comes-to-the-CPU-at-the-same-time >

< https://stackoverflow.com/questions/22638062/what-happens-when-two-interrupts-occur-at-the-same-time-in-linux-kernel >

< https://cs.stackexchange.com/questions/29846/can-an-interrupt-handler-be-preempted >


8) Can we call Sleep() inside an interrupt ( linux question )?

< https://www.quora.com/Why-cant-you-sleep-in-an-interrupt-handler-in-the-Linux-kernel-Is-this-true-of-all-OS-kernels >


9) what is interrupt latency and nested vectored interrupt controller ?

https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/beginner-guide-on-interrupt-latency-and-interrupt-latency-of-the-arm-cortex-m-processors >


10) What is priority inversion and what are possible solution for that ??

There are 2 methods to avoid priority inversion :

1) priority inheritance : Low priority task will take the priority of higher priority task , when high priority task is pending on resources.

2) priority celling : resources will have ( highest priority + 1 ) and accessing task will take priority of resource , however this will only occur when when is no pending task on resource.


11) Race condition and critical section :

https://practice.geeksforgeeks.org/problems/race-condition-in-os >

https://www.tutorialspoint.com/race-condition-critical-section-and-semaphore >


12) Deadlocks :

https://www.geeksforgeeks.org/introduction-of-deadlock-in-operating-system/ >

One of the common way to avoid deadlocks is to have preemption in system.


13) What are different type of schedulers available :

A scheduling algorithm is core and heart of any RTOS , there are 2 popular type of scheduler available:

1) preemptive scheduler

2) cooperative scheduler

However most of RTOS you find in real world are not pure preemptive or cooperative , but they are mix of both.


14) What is so "Real Time" in real time operating system |OR| what is difference between a normal OS and RTOS:

https://www.ni.com/en-in/innovations/white-papers/07/what-is-a-real-time-operating-system--rtos--.html >


15) Cache memory related questions :

Read about cache memory and must read about following terminology:

1) Cache hit , cache miss

2) Cache invalidation

3) Locality of Reference.

4) L1, L2, L3 cache.

5) Write through , write back

https://www.webopedia.com/TERM/C/cache.html >

https://www.geeksforgeeks.org/locality-of-reference-and-cache-operation-in-cache-memory/ >


16) What is difference between SRAM and DRAM :

https://www.geeksforgeeks.org/difference-between-sram-and-dram/ >


17) what is FRAM :

https://www.electronics-notes.com/articles/electronic_components/semiconductor-ic-memory/fram-ferroelectric-ram-memory.php >


18) NVMe , SATA , PCIe :

https://www.quora.com/Whats-the-difference-between-an-SSD-and-NVMe >


19) What is need of word alignment in compiler/computer architecture :

https://stackoverflow.com/questions/381244/purpose-of-memory-alignment >


20) What is difference between Static library and Dynamic library ?

Static and Dynamic Libraries | Set 1 - GeeksforGeeks >

https://medium.com/@StueyGK/static-libraries-vs-dynamic-libraries-af78f0b5f1e4 >



Saturday 12 December 2020

C interview questions - Part 3

 1) Difference between memcpy , memmove and memset :

All operations are done for memory copy. 

In case of memmove memory overlapping will be detected and adjustments will be made.

Behaviour of memcpy with overlapping is undefined. Not that it is undefined. I have personally tried this with different compilers and you cannot predict outpur of memcpy with overlapping.

memset is used to initialise memory with same integer.

see below example try it on different compiler.


2) NULL pointer in C :

NULL pointer in C is mostly defined as (void*)0; as it can be assigned to any other valid pointer type.

dereferancing NULL pointer can create system crash.

Size of NULL pointer is similar to any other pointer.

Try below program on Code Blocks + MinGW.

ref : < https://www.geeksforgeeks.org/few-bytes-on-null-pointer-in-c/


3) What are various way to intialise all elements of auto array with zero :


4) How to find number of element in an array


5) what is difference between strcpy() and strcat() function in C ?

They do different things. Use strcpy() if you want to copy a string and strcat() when you need to append a string to another one. Of course, you can use strcpy() instead of strcat() if you want (and don't mind the time needed for an additional function call to find the end of the first string) but there is no additional inherent safety in using one or the other.

ref : < https://bytes.com/topic/c/answers/216438-difference-between-strcpy-strcat >

proto types:

char* strcpy(char* destination, const char* source);

char *strcat(char *dest, const char *src)

ref < https://www.tutorialspoint.com/c_standard_library/c_function_strcpy.htm >

ref < https://www.tutorialspoint.com/c_standard_library/c_function_strcat.htm >


6) write a program to find if string is rotation of other string 

I have tried to write my copy with the help of this geek for geek article.

https://www.geeksforgeeks.org/a-program-to-check-if-strings-are-rotations-of-each-other/ >


7) write a program to find missing bracket in a string.

ref : < https://www.geeksforgeeks.org/check-for-balanced-parentheses-in-an-expression/ >

we will try to write this program in C and C++.

As C++ have readymade STL availible for stack.

C++ example

I have made little bit modification to make it little easy.

C example

The difference here is we do not have STL support in C , so we have to implement our out stack , so this can be a seprate question " implement stack in C". I have changed few names in geeksforgeeks implementation to make it more easy to understand.

8) Why mostly double pointers are used while implementing "linked list" or "stack" in C ?


9) why data alignment is required in C programming ?

we know that on 32-bit machines char are aligned on 1 byte boundry , shor int on 2 byte boundry , and long int on 4 byte boundry.


10) What is IEEE 754 float numbers and what are disadvantage of them and what are alternatives ??


11) why use of goto statement is discouraged in C language.


12) why multiple return statement in a single function is discouraged in C language.


13) 2 diamentional arrays in C language.


14) why int should not be used as bool in C language.


15) Swap all charaters in string ( like first with last and go on ) in C code :


16) Hamming code implementation in C :

https://www.geeksforgeeks.org/hamming-code-implementation-in-c-cpp/ >


17) write a code to caluculate redundancy from 3 sets of bit :

 




C interview questions - Part 2

Please share it with your friends who are preparing for interview , and comment down your suggestions , or any mistake in the post. 

code snippets are hosted on ideone.com. you can run code there as well.


1. What will be new average given ( old average + number of samples ) and new sample value :

New average = ( old average ) * ( old number of samples ) + new sample / ( old number of samples + 1 ) 


2. Read some details about C standard libraries : 

https://www.tutorialspoint.com/c_standard_library/ >


3. Remeber that strings are terminated with null character "\0" (zero) , this is how we find end of the string. In C language there is no seperate string data type , strings are created using arrays. Where as in C++ , strings are a seperate class.

also '\0'( ASCII ) is 0x00 in hex.

'0'( ASCII ) is 0x30 in hex.

one interesting string question < https://stackoverflow.com/questions/18688971/c-char-array-initialization/18688992#18688992 >

Consider below example of "strlen" vs "sizeof" below.



4. What is use of Assert for error handling in C language ???

C assert() statement is a runtime assert statement i.e it won't give you a compile time error , but it will result into runtime error when assert() statement fails.

Compile time asserts can be also written , using preprocessor directives like #if , #warning, #error or using if...else. Always remember preprocessor is invoked before compiler and linker.

Please play around with below example code , I would recommend to install codeblocks + MinGW in yout machine and try there.


5. What is use of Cygwin ( note : windows bash is going to replace Cygwin ).

https://www.quora.com/For-what-purposes-is-cygwin-exactly-used >


6. Overflow of smaller data type:

Remeber that when you assign a "larger value" to a "smaller data type variable" overflow occurs.

Also remember that range of signed char is -128 to 127 ( negative side is more ) and unsigned char range is 0 to 255.

https://stackoverflow.com/questions/21667971/what-happens-when-a-char-is-assigned-a-value-too-large-to-fit-in-a-byte >

consider following example :


7. Rules of integer promotion  and type casting in C :

This is excellent article.

https://www.geeksforgeeks.org/integer-promotions-in-c/ >

also try this code as well:


Is integer promotion always needed ? 

https://stackoverflow.com/questions/12807953/do-integer-promotions-always-happen-when-evaluating-an-expression >


8. Types of Error in C :

There are 3 types of errors : compile time , link time , run time , logical/design errors.

compile time error can be syntex error or semantic error.

It's very difficult to detect design errors.

https://www.geeksforgeeks.org/errors-in-cc/ >

example of sematic error :

int main(void) {

    volatile unsigned int temp;

    10 = temp;

}

error: lvalue required as left operand of assignment.

9. what is lvalue and rvalue ??

https://www.geeksforgeeks.org/lvalue-and-rvalue-in-c-language/ >

10. How would you avoid invalid use of assignment operator instead of comparison operator ??

consider below nasty situation and try to guess output :

to avoid this you can make rule to write VALUES/rvalue on left hand side of comparision operator. as writng rvalue on left hand side of assignment operator will result in simantic error.

You should write following way.

11. Write a code to convert big Endian to little endian and vice versa :



12. operator precedence in c :

while it is better to us brackets to isolate statements in C , knowledge of operator precedence is necessary to avoid unnecessary use of brackets.



13. Dividend ÷ Divisor = Quotient ( and reminder ) , numerators and denominators.









remember that % will take the sign of dividend ( this is reminder ) 

14. Duff's device:


15. coroutine vs subroutine vs threads :



16. write a program to find a number is power of 2 :

here the trick is number 2 , note that if a given number is power of 2 then that number will have only 1 bit set in it's binanry representation. 



17. Write a generic program to find if  given number "pow" is power of number "num":

This cannot be achived by the technique above. And you have to go traditional way of dividing numbers or multiplying the numbers and then comparing them.


18. Write a C program to swap all nibbles in individual byte in an 32-bit integer.



19. Write a macro to define a register variable in C.


20. what is stack overflow and heap overflow ?


21. does malloc always returns contiguous memory ?

Answer is YES.


Others :
I would strongly recommend to read SO anweres of this account.

Please share it with your friends who are preparing for interview , and comment down your suggestions , or any mistake in the post. 

code snippets are hosted on ideone.com. you can run code there as well.