Dear Readers, Welcome to DS Interview Questions and Answers have been designed specially to get you acquainted with the nature of questions you may encounter during your Job interview for the subject of DS. These DS Questions are very important for campus placement test and job interviews. As per my experience good interviewers hardly plan to ask any particular questions during your Job interview and these model questions are asked in the online technical test and interview of many IT companies.
Data Structures is defined as the way of organizing all data items that consider not only the elements stored but also stores the relationship between the elements.
Primary data structures are the basic data structures that directly operate upon the machine instructions. All the basic constants (integers, floating-point numbers, character constants, string constants) and pointers are considered as primary data structures.
A data structure formed when the number of data items are known in advance is referred as static data structure or fixed size data structure.
Some of the static data structures in C are arrays, pointers, structures etc.
A data structure formed when the number of data items are not known in advance is known as dynamic data structure or variable size data structure.
Some of the dynamic data structures in C are linked lists, stacks, queues, trees etc.
Linear data structures are data structures having a linear relationship between its adjacent elements. Eg) Linked lists
Non-linear data structures are data structures that don’t have a linear relationship between its adjacent elements but have a hierarchical relationship between the elements.
Eg: Trees and Graphs
Many languages such as BASIC and FORTRAN do not support pointers. If linked lists are required and pointers are not available, then an alternative implementation must be used known as cursor implementation.
The different types of linked list include singly linked list, doubly linked list and circular linked list.
The basic operations carried out in a linked list include:
• Creation of a list
• Insertion of a node
• Deletion of a node
• Modification of a node
• Traversal of the list
• It is not necessary to specify the number of elements in a linked list during its declaration
• Linked list can grow and shrink in size depending upon the insertion and deletion that occurs in the list
• Insertions and deletions at any place in a list can be handled easily and efficiently
• A linked list does not waste any memory space
• Searching a particular element in a list is difficult and time consuming
• A linked list will use more storage space than an array to store the same number of elements
Some of the important applications of linked lists are manipulation of polynomials, sparse matrices, stacks and queues.
Stack is an ordered collection of elements in which insertions and deletions are restricted to one end. The end from which elements are added and/or removed is referred to as top of the stack. Stacks are also referred as piles, push-down lists and last-in-first-out (LIFO) lists.
The basic operations that can be performed on a stack are
• Push operation
• Pop operation
• Peek operation
• Empty check
• Fully occupied check
The different ways of representing expressions are
• Infix Notation
• Prefix Notation
• Postfix Notation
• It is the mathematical way of representing the expression
• It is easier to see visually which operation is done from first to last
• Need not worry about the rules of precedence
• Need not worry about the rules for right to left associativity
• Need not need parenthesis to override the above rules
• Fully parenthesize the expression starting from left to right. During parenthesizing, the operators having higher precedence are first parenthesized
• Move the operators one by one to their right, such that each operator replaces their corresponding right parenthesis
• The part of the expression, which has been converted into postfix is to be treated as single operand
• Once the expression is converted into postfix form, remove all parenthesis
• Fully parenthesize the expression starting from left to right. During parenthesizing, the operators having higher precedence are first parenthesized
• Move the operators one by one to their left, such that each operator replaces their corresponding left parenthesis
• The part of the expression, which has been converted into prefix is to be treated as single operand
• Once the expression is converted into prefix form, remove all parenthesis
The difference between stacks and linked lists is that insertions and deletions may occur anywhere in a linked list, but only at the top of the stack
• It is not necessary to specify the number of elements to be stored in a stack during its declaration, since memory is allocated dynamically at run time when an element is added to the stack
• Insertions and deletions can be handled easily and efficiently
• Linked list representation of stacks can grow and shrink in size without wasting memory space, depending upon the insertion and deletion that occurs in the list
• Multiple stacks can be represented efficiently using a chain for each stack
Queue is an ordered collection of elements in which insertions are restricted to one end called the rear end and deletions are restricted to other end called the front end. Queues are also referred as First-In-First-Out (FIFO) Lists.
Priority queue is a collection of elements, each containing a key referred as the priority for that element. Elements can be inserted in any order (i.e., of alternating priority), but are arranged in order of their priority value in the queue. The elements are deleted from the queue in the order of their priority (i.e., the elements with the highest priority is deleted first). The elements with the same priority are given equal importance and processed accordingly.
The difference between queues and linked lists is that insertions and deletions may occur anywhere in the linked list, but in queues insertions can be made only in the rear end and deletions can be made only in the front end.
Deque (Double-Ended Queue) is another form of a queue in which insertions and deletions are made at both the front and rear ends of the queue. There are two variations of a deque, namely, input restricted deque and output restricted deque. The input restricted deque allows insertion at one end (it can be either front or rear) only. The output restricted deque allows deletion at one end (it can be either front or rear) only.
A data structure helps you to understand the relationship of one data element with the other and organize it within the memory. Sometimes the organization might be simple and can be very clearly visioned. Eg) List of names of months in a year –Linear Data Structure, List of historical places in the world- Non-Linear Data Structure. A data structure helps you to analyze the data, store it and organize it in a logical and mathematical manner.
• To identify and create useful mathematical entities and operations to determine what classes of problems can be solved using these entities and operations
• To determine the representation of these abstract entities and to implement the abstract operations on these concrete representation
• Definition of Stack
• Operations of Stack: PUSH and POP
• Example
• Definition of Expression
• Types of expression
• Algorithm for infix to postfix expression
• Example
• Definition of Queue
• Operations of Queue: insert and remove
• Example
• Evaluating arithmetic expression
• Balancing the symbols
• Function calls
• Introduction to Doubly linked list
• Operations: insertion and deletion with algorithm
• Linked list implementation of Stack
• Linked list implementation of Queue
• Preorder traversal
• Inorder traversal
• Postorder traversal
• Levelorder traversal
• Visiting a node
• Traverse the left sub-tree
• Traverse the right sub-tree
• Process the root node
• Traverse the left sub-tree
• Traverse the right sub-tree
• Traverse the left sub-tree
• Process the root node
• Traverse the right sub-tree
• Traverse the left sub-tree
• Traverse the right sub-tree
• Process the root node
• Storage method is easy and can be easily implemented in arrays
• When the location of a parent/child node is known, other one can be determined easily
• It requires static memory allocation so it is easily implemented in all programming language
Insertions and deletions in a node take an excessive amount of processing time due to data movement up and down the array.
Insertions and deletions in a node involve no data movement except the rearrangement of pointers, hence less processing time.
• Given a node structure, it is difficult to determine its parent node
• Memory spaces are wasted for storing null pointers for the nodes, which have one or no sub-trees
• It requires dynamic memory allocation, which is not possible in some programming language
A binary search tree is a special binary tree, which is either empty or it should satisfy the following characteristics:
• Every node has a value and no two nodes should have the same value i.e) the values in the binary search tree are distinct
• The values in any left sub-tree is less than the value of its parent node
• The values in any right sub-tree is greater than the value of its parent node
• The left and right sub-trees of each node are again binary search trees
General tree is a tree with nodes having any number of children.
In threaded binary tree, the NULL pointers are replaced by some addresses. The left pointer of the node points to its predecessor and the right pointer of the node points to its successor.
An expression tree is a tree which is build from infix or prefix or postfix expression. Generally, in such a tree, the leaves are operands and other nodes are operators.
Right-in threaded binary tree is defined as one in which threads replace NULL pointers in nodes with empty right sub-trees.
Left-in threaded binary tree is defined as one in which each NULL pointers is altered to contain a thread to that node’s inorder predecessor.
• Definition of binary search tree
• Operations of binary search tree: Insertion and Deletion
• Example
• Definition of tree traversal
• Types of traversals
• Example
• Definition of threaded binary tree
• Operations of threaded binary tree: insertion
• Example
• Procedure to construct expression tree
• Example
• Steps
• Example