+012 345 6789 | placeprep@gmail.com
Learn Algorithms

Once you have cleared the concepts of Data Structures, now its time to start your journey through the Algorithms. Based on the type of nature and usage, the Algorithms are grouped together into several categories, as shown below:

1. Searching Algorithm

Searching algorithms are used to find a specific element in an array, string, linked list, or some other data structure.

The most common searching algorithms are:

  • Linear Search –In this searching algorithm, we check for the element iteratively from one end to the other.
  • Binary Search – In this type of searching algorithm, we break the data structure into two equal parts and try to decide in which half we need to find for the element.
  • Ternary Search – In this case, the array is divided into three parts, and based on the values at partitioning positions we decide the segment where we need to find the required element.

2. Sorting Algorithm

Sorting Algorithm is used to rearrange a given array or list elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of element in the respective data structure.

There are a lot of different types of sorting algorithms. Some widely used algorithms are:

  • Bubble Sort
  • Selection Sort
  • Insertion Sort
  • Quick Sort
  • Merge Sort

3. Divide and Conquer Algorithm

Divide and Conquer is an algorithmic paradigm. A typical Divide and Conquer algorithm solves a problem using following three steps.

  1. Divide: Break the given problem into subproblems of same type.
  2. Conquer: Recursively solve these subproblems
  3. Combine: Appropriately combine the answers

This is the primary technique mentioned in the two sorting algorithms Merge Sort and Quick Sort which are mentioned earlier. To learn more about the technique, the cases where it is used, and its implementation and solve some interesting problems, please refer to the dedicated article Divide and Conquer Algorithm.


4. Greedy Algorithms

As the name suggests, this algorithm builds up the solution one piece at a time and chooses the next piece which gives the most obvious and immediate benefit i.e., which is the most optimal choice at that moment. So the problems where choosing locally optimal also leads to the global solutions are best fit for Greedy.

For example, consider the Fractional Knapsack Problem. The local optimal strategy is to choose the item that has maximum value vs weight ratio. This strategy also leads to a globally optimal solution because we are allowed to take fractions of an item.


5. Recursion

Recursion is one of the most important algorithms which uses the concept of code reusability and repeated usage of the same piece of code.


6. Backtracking Algorithm

Backtracking is an algorithmic technique for solving problems recursively by trying to build a solution incrementally, one piece at a time, removing those solutions that fail to satisfy the constraints of the problem at any point of time


7. Dynamic Programming

The main concept of the Dynamic Programming algorithm is to use the previously calculated result to avoid repeated calculations of the same subtask which helps in reducing the time complexity.


8. Pattern Searching

The Pattern Searching algorithms are sometimes also referred to as String Searching Algorithms and are considered as a part of the String algorithms. These algorithms are useful in the case of searching a string within another string.

9. Bitwise Algorithms

The Bitwise Algorithms is used to perform operations at the bit-level or to manipulate bits in different ways. The bitwise operations are found to be much faster and are sometimes used to improve the efficiency of a program.

For example: To check if a number is even or odd. This can be easily done by using Bitwise-AND(&) operator. If the last bit of the operator is set than it is ODD otherwise it is EVEN. Therefore, if num & 1 not equals to zero than num is ODD otherwise it is EVEN.

Practice Problems on Data Structures and Algorithms (DSA)
Top Interview Questions
 

What is a Data Structure? 
A data structure is a way of organizing data so that the data can be used efficiently. Different kinds of data structures are suited to different kinds of applications, and some are highly specialized for specific tasks. For example, B-trees are particularly well-suited for the implementation of databases, while compiler implementations usually use hash tables to look up identifiers. (Source: Wiki Page

What are linear and non-linear data Structures? 

  • Linear: A data structure is said to be linear if its elements form a sequence or a linear list. Examples: Array. Linked List, Stacks and Queues
  • Non-Linear: A data structure is said to be non-linear if the traversal of nodes is nonlinear in nature. Example: Graph and Trees.
 

What are the various operations that can be performed on different Data Structures? 

  • Insertion ? Add a new data item in the given collection of data items.
  • Deletion ? Delete an existing data item from the given collection of data items.
  • Traversal ? Access each data item exactly once so that it can be processed.
  • Searching ? Find out the location of the data item if it exists in the given collection of data items.
  • Sorting ? Arranging the data items in some order i.e. in ascending or descending order in case of numerical data and in dictionary order in case of alphanumeric data.

How is an Array different from Linked List? 

  • The size of the arrays is fixed, and Linked Lists are Dynamic in size.
  • Inserting and deleting a new element in an array of elements is expensive, Whereas both insertion and deletion can easily be done in Linked Lists.
  • Random access is not allowed on Linked Listed.
  • Extra memory space for a pointer is required with each element of the Linked list.
  • Arrays have a better cache locality that can make a pretty big difference in performance.

What is Stack and where it can be used? 
Stack is a linear data structure in which the order LIFO(Last In First Out) or FILO(First In Last Out) for accessing elements. Basic operations of the stack are: Push, Pop, Peek 

Applications of Stack: 

  1. Infix to Postfix Conversion using Stack
  2. Evaluation of Postfix Expression
  3. Reverse a String using Stack
  4. Implement two stacks in an array
  5. Check for balanced parentheses in an expression

What is a Queue, how it is different from the stack and how is it implemented? 
The queue is a linear structure that follows the order is First In First Out (FIFO) to access elements. Mainly the following are basic operations on queue: Enqueue, Dequeue, Front, Rear 
The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added. Both Queues and Stacks can be implemented using Arrays and Linked Lists. 

 What are Infix, prefix, Postfix notations? 

  • Infix notation: X + Y – Operators are written in between their operands. This is the usual way we write expressions. An expression such as
   A * ( B + C ) / D
  • Postfix notation (also known as “Reverse Polish notation”): X Y + Operators are written after their operands. The infix expression given above is equivalent to
   A B C + * D/
  • Prefix notation (also known as “Polish notation”): + X Y Operators are written before their operands. The expressions given above are equivalent to
   / * A + B C D

Converting between these notations: Click here 

What is a Linked List and What are its types? 

A linked list is a linear data structure (like arrays) where each element is a separate object. Each element (that is node) of a list is comprised of two items – the data and a reference to the next node. Types of Linked List : 

  1. Singly Linked List : In this type of linked list, every node stores address or reference of the next node in the list and the last node has next address or reference as NULL. For example 1->2->3->4->NULL
  2. Doubly Linked List : Here, here are two references associated with each node, One of the reference points to the next node and one to the previous node. Eg. NULL<-1<->2<->3->NULL
  3. Circular Linked List : Circular linked list is a linked list where all nodes are connected to form a circle. There is no NULL at the end. A circular linked list can be a singly circular linked list or a doubly circular linked list. Eg. 1->2->3->1 [The next pointer of the last node is pointing to the first]

Which data structures are used for BFS and DFS of a graph? 

Can doubly-linked be implemented using a single pointer variable in every node? 
A doubly linked list can be implemented using a single pointer. See XOR Linked List – A Memory Efficient Doubly Linked List 

How to implement a stack using queue? 
A stack can be implemented using two queues. Let stack to be implemented be ‘s’ and queues used to implement be ‘q1’ and ‘q2’. Stack ‘s’ can be implemented in two ways: 

How to implement a queue using a stack? 
A queue can be implemented using two stacks. Let the queue to be implemented be q and the stacks used to implement q be stack1 and stack2. q can be implemented in two ways: 

Which Data Structure Should be used for implementing LRU cache? 
We use two data structures to implement an LRU Cache.

  1. Queue which is implemented using a doubly-linked list. The maximum size of the queue will be equal to the total number of frames available (cache size). The most recently used pages will be near the rear end and the least recent pages will be near the front end.
  2. A Hash with page number as key and address of the corresponding queue node as value. See How to implement LRU caching scheme? What data structures should be used?

How to check if a given Binary Tree is BST or not? 
If inorder traversal of a binary tree is sorted, then the binary tree is BST. The idea is to simply do inorder traversal and while traversing keep track of previous key value. If current key value is greater, then continue, else return false. See A program to check if a binary tree is BST or not for more details. 

Linked List Questions 

Tree Traversal Questions 

Convert a DLL to Binary Tree in-place 
See In-place conversion of Sorted DLL to Balanced BST 

Convert Binary Tree to DLL in-place 
See Convert a given Binary Tree to Doubly Linked List | Set 1, Convert a given Binary Tree to Doubly Linked List | Set 2 

Delete a given node in a singly linked list 
Given only a pointer to a node to be deleted in a singly linked list, how do you delete it? 

Reverse a Linked List 
Write a function to reverse a linked list 

Detect Loop in a Linked List 
Write a C function to detect loop in a linked list

Which data structure is used for dictionary and spell checker? 
Data Structure for Dictionary and Spell Checker? 

PlacePrep

Accusam nonumy clita sed rebum kasd eirmod elitr. Ipsum ea lorem at et diam est, tempor rebum ipsum sit ea tempor stet et consetetur dolores. Justo stet diam ipsum lorem vero clita diam

Newsletter

Get In Touch

123 Street,Pune, Maharashtra

+012 345 67890

placeprep@gmail.com

Our Subjects

Copyright © PlacePrep. All Rights Reserved.