Process Cleanups Via Exit Handlers Registered Using atexit function

Any normal Unix process can terminate in following ways:

  • Graceful exit – Normal termination of process. For eg:
    • Returning from main, calling exit etc.
  • Ungraceful exit – Abnormal termination of process. For eg:
    • Abort signal. Due to some fatal error etc.

_exit, _EXIT and exit functions can be used to terminate the process normally. Usually exit functions do the cleanup but if needed a programmer can also define its own exit handler functions to do some specific cleanup actions needed by the program.

(more…)
Process Cleanups Via Exit Handlers Registered Using atexit function Read More

Find Minimum Operations Required To Convert String1 To String2

Write a program to find out the minimum number of operations required to convert source string to target string.
To convert source string to target string, only insert and delete character operation are allowed.
For eg:

  • Source = “ABCD”, Target = “ACDB” – Operations = 2 (Delete B and insert B at end)
  • Source = “ABCDEF”, Taget = “ACDEF” – Operations = 1 (Delete B)
(more…)
Find Minimum Operations Required To Convert String1 To String2 Read More

Find 3 Peak And Valley In An Array Separated By N Indices

Given an array of size n, we have to find 3 peak values and 3 trough values in the array such that each peak values are separated by another peak value by n entries.
Similarly, each trough value must be n days separated from the other trough values. For eg:

int arr[] = {1, 5, 6, 4, 3, 3, 10, 2, 8, 6, 11, 4, 10};
int distance = 2;

In above example, peak values are – 6, 10 and 11 (which are 2 indices apart).

valley values are – 1, 3 and 2 (which are 2 indices apart).

(more…)
Find 3 Peak And Valley In An Array Separated By N Indices Read More

Find Missing and Duplicate Number In An Array

Given an unsorted array of size n. Array elements are in range from 1 to n. One number from set {1, 2, …n} is missing and one number occurs twice in array. We must write a program to find the missing and duplicate numbers. For eg:

Int a[] = {1,2,3,4,6,7,8,9,10,4}; 

As you can see in above example, missing number is “5” and duplicate number is “4”.

(more…)
Find Missing and Duplicate Number In An Array Read More

Connected Graph Property Explained With Simple Example

Graph is a data structure which consists of a set of vertices which is called as Node, together with a set of collection of pair of vertices which is called as an Edge.
A graph data structure can be represented as a pair (V, E) where V is a set of nodes called vertices and E is a collection of pairs of vertices called edges. Graphs are used to solve many real-life problems such as fastest ways to go from A to B etc.

A Graph is called connected graph if each of the vertices of the graph is connected from each of the other vertices which means there is a path available from any vertex to any other vertex in the Graph.

Before going ahead have a look into Graph Basics.

(more…)
Connected Graph Property Explained With Simple Example Read More

Topological Sort Explained With Simple Example

Topological sort is a method to sort the vertices in directed acyclic graph in which each node comes before all the nodes to which it has edges going to. Topological sort is mainly used in cases where a certain node can be visited if and only if certain nodes has been visited before.

A Directed Acyclic graph or DAG is a graph which doesn’t have any cycle.

All pairs of consecutive vertices in topological sorted order are connected by edges which forms a directed Hamiltonian Path.

(more…)
Topological Sort Explained With Simple Example Read More

Dynamic Memory Allocation Explained With Simple Example

Dynamic memory allocation is the process of allocating memory at run time by the program based on the need of the program. Dynamic Memory allocation is done from the heap memory available in the system and handling of memory (creation and deletion) is totally handled by the programmer. In case, programmer forgets to cleanup the allocated dynamic memory block, it will lead into memory leak and this memory block will be blocked for further use until program restarts.

Methods available to allocate dynamic memory at run time are

  • malloc () – allocates one memory block of requested size.
  • calloc () – allocates multiple blocks of memory and initialize it with 0.
  • realloc () – increases or decreases the size of previously allocated memory block.
  • free () – frees the memory allocated by above functions.
(more…)
Dynamic Memory Allocation Explained With Simple Example Read More

Early Binding vs Late Binding in C++: vtable and vptr Explained

Binding means deciding which function should be called for a particular function call. In C++, this decision can happen either at compile time or at run time.
When the function call is resolved at compile time, it is called Early Binding or Static Binding.
When the function call is resolved at run time based on the actual object type, it is called Late Binding or Dynamic Binding.

Let’s look into the differences first.

Early BindingLate Binding
Happens at compile time.Happens at run time.
Also called Static BindingAlso called Dynamic Binding
Compiler has all information to invoke correct function version at compile time.Compiler doesn’t have information to identify correct function version.
Normal function calls are example of Early Binding.Virtual functions are example of Late Binding.
Functions invoked by object is example of early bindingFunctions invoked by pointers can be example of late binding.
Early binding is efficient as no cost paid at run-time for function resolution.Late Binding is slightly costly as function resolution happens at run time.
Early binding doesn’t provide flexibility of one method multiple interfaces.Late Binding provide flexibility.
Doesn’t have any virtual table or virtual pointer.It uses Virtual Table and Virtual Pointer.
(more…)
Early Binding vs Late Binding in C++: vtable and vptr Explained Read More