# DataStructures / Data structures Interview questions

Could not find what you were looking for? send us the question and we would be happy to answer your question.

What is data structure?

Data structures refers to the way data is organized and manipulated. It helps to find ways to make data access more efficient. When dealing with data structure, we not only focus on one piece of data, but rather different set of data and how they can relate to one another in an organized manner.

Define linear data structure.

A linear data structure traverses the data elements sequentially, in which only one data element can directly be reached. For example Arrays, Linked Lists are linear data structure.

Define Non-Linear data structure.

The data items are not arranged in a sequential structure. Every data item is attached to several other data items in a way that is specific for reflecting relationships. Graph and Tree are examples of non linear data structure.

Mention different data structures.

Commonly used data structures are,

• list,
• arrays,
• stack,
• queues,
• graph,
• tree.
What are the operations that can be performed on a data-structure?

The following operations are commonly performed on any data-structure,

Deletion: removing a data item.

Traversal: accessing and/or printing all data items.

Searching: finding a particular data item.

Sorting: arranging data items in a pre-defined sequence.

What is an Array?

Array is a data structure used to store homogeneous elements at contiguous locations. Size of an array must be provided before storing data.

A linked list is a linear data structure similar to arrays where each element is a separate object. Each element or node of a list comproses of two items ? the data and a reference to the next node.

What are the different types of linked list?

There are 3 types of linked list.

Singly Linked List: Every node stores address or reference of next node in list and the last node has next address or reference as NULL.

Doubly Linked List: There are two references associated with each node, one of the references points to the next node and other to the previous node. Advantage of this data structure is that we can traverse in both the directions and for deletion we don't need to have explicit access to previous node.

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 doubly circular linked list. Advantage of this data structure is that any node can be made as starting node. This is useful in implementation of circular queue in linked list.

How do I find middle element of linked list?

To find the middle element of linked list in one pass, you may maintain two-pointer, one increment at each node while other increment after two nodes at a time so that when first pointer reaches end, second pointer will point to middle element of linked list.

Define Stack in data structures.

A stack or LIFO (last in, first out) is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the last element that was added. In stack both the operations of push and pop takes place at the same end that is top of the stack. It can be implemented by using both array and linked list.

Explain queue in data structure.

A queue or FIFO (first in, first out) is an abstract data type that serves as a collection of elements, with two principal operations: enqueue , the process of adding an element to the collection.(The element is added from the rear side) and dequeue , the process of removing the first element that was added. (The element is removed from the front side). It can be implemented by using both array and linked list.

Why do we need stack data structure?

Stack implements LIFO method, addition and retrieval of a data item takes only Ο(1) time. Stack is used where we need to access data in the reverse order or its arrival order. Stacks are used commonly in recursive function calls, expression parsing, depth first traversal of graphs etc.

Minimum number of queues needed to implement the priority queue.

Two. One queue is used for actual storing of data and another for storing priorities.

Applications of tree data-structure.
• The manipulation of Arithmetic expression,
• Symbol Table construction,
• and Syntax analysis.
What is priority queue?

A priority queue is a collection of elements such that each element has been assigned a priority.

What is the difference between Stack and Queue data structure?

Stack is LIFO (Last In First Out) data structure while Queue is a FIFO (First In First Out) data structure.

The main difference between singly linked list and doubly linked list is the ability to traverse. In a single linked list, node only points towards next node, and there is no pointer to previous node, which means you can not traverse back on a singly linked list. On the other hand doubly linked list maintains two pointers, towards next and previous node, which allows you to navigate in both direction in any linked list.

What is binary search tree?

Binary Search Tree has some special properties such as left nodes contains items whose value is less than root, right sub tree contains keys with higher node value than root, and there will not be any duplicates in the tree.

Binary Search Tree is also known as ordered or sorted binary trees.

List out few areas in which data structures are applied extensively?
• Compiler Design,
• Operating System,
• Database Management System,
• Statistical analysis package,
• Numerical Analysis,
• Graphics,
• Artificial Intelligence,
• and Simulation.
What is recursive data structure?

A data structure that is partially composed of smaller or simpler instances of the same data structure. For instance, a tree is composed of smaller trees (subtrees) and leaf nodes, and a list may have other lists as elements.

Give few examples of mathematical function that can be performed recursively?

Many mathematical functions can be defined recursively such as,

• Factorial,
• Fibonacci,
• Euclid's GCD (greatest common denominator),
• Fourier Transform.
Is sorting possible with delete operation in data structure?

Sorting is not possible in Deletion. Using insertion we can perform insertion sort, using selection we can perform selection sort, using exchange we can perform the bubble sort. But no sorting method can be done just using deletion.

List out few applications that make use of Multilinked Structures?
• Sparse matrix,
• Index generation.
In RDBMS, what is the efficient data structure used in the internal storage representation?

B+ tree. in B+ tree, all the data is stored only in leaf nodes, that makes searching easier. This corresponds to the records that shall be stored in leaf nodes.

Describe binary tree and its property.

In a binary tree a node can have maximum two children, or in other words we can say a node can have 0,1, or 2 children.

Its properties are as follows.

The maximum number of nodes on any level i is 2i where i>=0.

The maximum number of nodes possible in a binary tree of height h is 2h-1.

The minimum number of nodes possible in a binary tree of height h is equal to h.

If a binary tree contains n nodes then its maximum possible height is n and minimum height possible is log2 (n+1).

If n is the total no of nodes and e is the total no of edges then e=n-1.The tree must be non-empty binary tree.

If n0 is the number of nodes with no child and n2 is the number of nodes with 2 children, then n0=n2+1.

What are multidimensional arrays?

Multidimensional arrays make use of multiple indexes to store data. It is useful when storing data that cannot be represented using a single dimensional indexing, such as data representation in a chess board game, tables with data stored in more than one column for example geo spatial data.

Are linked lists considered linear or non-linear data structure?

Based on storage, a linked list is considered non-linear. However on the basis of access strategies a linked list is considered linear.

What is dynamic data structure?

Dynamic data structure expands and shrinks in size as program runs. It provides a flexible means of manipulating data as it can adjust capacity according to the size of the data.

Differentiate Stack and Array.

Data that is stored in a stack follows a LIFO pattern. This means that data access follows a sequence wherein the last data to be stored will the first one to be extracted. Arrays, on the other hand, does not follow a particular order and instead can be accessed by referring to the indexed element within the array.

What is a dequeue?

A dequeue is a double-ended queue in which elements can be inserted or removed from either end.

What is a graph?

A graph is a data structure that contains a set of ordered pairs. These ordered pairs are also referred as edges or arcs, and are used to connect nodes where data can be stored and retrieved.

What is an AVL tree?

An AVL tree is a type of binary search tree that is always in a state of partially balanced. The balance is measured as a difference between the heights of the subtrees from the root. This self-balancing tree was known to be the first data structure to be designed as such.

AVL Tree is Named after its inventor Adelson, Velski & Landis, are height balancing binary search tree.

What operations can be performed on stack data structure?

push: adds an item to stack.

pop: removes the top stack item.

peek: retrieves the value of top item without removing it.

What is linear search?

Linear search for an item in a sequentially arranged data type. These sequentially arranged data items known as array or list, are accessible in a incremental memory location.

Linear search compares expected data item with each of data items in list or array. The average case time complexity of linear search is Ο(n) and worst case complexity is Ο(n2). Data in target arrays/lists need not have to be sorted.

Explain binary search.

A binary search works only on sorted lists or arrays. This search starts at the middle element which splits the entire list into two parts. This search first compares the target value to the mid of the list. If it is not found, then it takes decision on whether to search on first part or second part if the search value is lesser than middle element or greater vice versa.

Binary search is also known as half-interval search or logarithmic search.

Heterogeneous Linked List is a linked list data-structure that contains or is capable of storing data for different datatypes.

Which data structures are used with the following areas: RDBMS, Network data model and hierarchical data model?

RDBMS implements Array data structure.

Network data model uses Graph.

Hierarchal data model uses Trees.

What is a postfix expression?

An expression in which each operator follows its operand is known as postfix expression. The main benefit of this expression is that there is no need to group sub-expressions in parentheses or to consider operator precedence.

What is a spanning Tree?

A spanning tree is a tree associated with a network. All the nodes of the graph appear on the tree once. A minimum spanning tree is a spanning tree organized so that the total edge weight between nodes is minimized.

What does a linked list node consist of?

A singly list node consists of two fields:data field to store the element and link field to store the address of the next node.

What are the different types of binary tree?

A rooted binary tree has a root node and every node has at most two children.

A full binary tree (proper or plane binary tree) is a tree in which every node in the tree has either 0 or 2 children.

A Complete Binary Tree has all levels completely filled except possibly the last level and the last level has all keys as left as possible.

In Perfect Binary Tree all internal nodes have two children and all leaves are at same level.

A binary tree is balanced if height of the tree is O(Log n) where n is number of nodes.

A degenerate (or pathological) tree is a tree where every internal node has one child. Such trees are performance-wise same as linked list.

What is tree traversal?

Tree traversal is the process to visit all the nodes of a tree. All the nodes are connected via edges (links), we always start from the root (head) node. There are three ways which we use to traverse a tree.

• In-order Traversal,
• Pre-order Traversal,
• Post-order Traversal,
What is a minimum spanning tree (MST) ?

In a weighted graph, a minimum spanning tree is a spanning tree that has minimum weight that all other spanning trees of the same graph.

What is hashing?

Hashing is a technique that converts a range of key values into a range of indexes of an array. By using hash tables, we can create an associative data storage where data index can be identified by providing its key values.

What is algorithm?

Algorithm is a step by step procedure, which defines a set of instructions to be executed in certain order to get the desired output.

What is the time complexity of Algorithm?

Time complexity of an algorithm indicates the total time required by the program to run to completion. It is usually expressed by using the big O notation.

Explain asymptotic analysis of an algorithm?

Asymptotic analysis of an algorithm refers to defining the mathematical boundation/framing of its run-time performance. Using asymptotic analysis, we can identify the best case, average case, and worst case scenario of an algorithm.

Asymptotic analysis is input bound so that if there's no input to the algorithm, it is considered to work in a constant time. Other than the "input" all other factors are considered constant.

Mention the criteria for algorithmic analysis.

An algorithm are generally analyzed on 2 factors, time and space. How much execution time and how much extra space required by the algorithm forms basis for analysis.

Explain Quick Sort algorithm.

Quick sort algorithm uses divide and conquer approach. It divides the list into smaller 'partitions' using 'pivot'. The values which are smaller than the pivot are arranged in the left partition and greater values are arranged in the right partition. Each partition is recursively sorted using quick sort.

Explain bubble sort algorithm.

Bubble sort is a comparison based algorithm in which each pair of adjacent elements is compared and elements are swapped if they are not in order. Bubble sort has the time complexity of Ο(n2), it is the least used sorting strategy for large volume data.

How insertion sort algorithm works?

Insertion sort divides the list into two sub-list, sorted and unsorted. It takes one element at time and finds it appropriate location in sorted sub-list and insert it to the sorted sub-list. It iteratively works on all the elements of unsorted sub-list and inserts them to sorted sub-list in order.

Insertion sort builds the final sorted list one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort.

Explain selection sort algorithm.

List is divided into two parts, the sorted part at the left end and the unsorted part at the right end. It selects the smallest element from unsorted sub-list and places it into the sorted list. This iterates unless all the elements from unsorted sub-list are consumed into sorted sub-list.

This algorithm is not suitable for large data sets as its average and worst case complexities are of Ο(n2), where n is the number of items.

Explain merge sort algorithm.

Merge sort algorithm is based on divide and conquer programming approach. This algorithm divides the unsorted list into n sublists, each containing 1 element and repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining.

What is shell sort?

Shell sort is an unstable quadratic sorting algorithm and it is a generalization of insertion sort.

This algorithm starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared.

How quick sort works?

Quick sort uses divide and conquer approach and this algorithm divides the list in smaller 'partitions' using 'pivot'. The values which are smaller than the pivot are arranged in the left partition and greater values are arranged in the right partition. Each partition is recursively sorted using quick sort.

What is recursive function?

A recursive function is a function that makes a call to itself. To prevent infinite recursion a conditional statement is placed that prevent the further calls. For example Fibonacci series can be determined using recursive function.

What is Huffmans algorithm?

Huffmans algorithm is associated in creating extended binary trees that has minimum weighted path lengths from the given weights. It makes use of a table that contains frequency of occurrence for each data element.

Which algorithm used in solving the 8 Queens problem.

Backtracking.

Difference between a Tree and Graph in Data structure.

 Tree. Graph. Tree is a special form of graph also called as minimally connected graph having only one path between any two vertices. A graph vertices can have more than one path, that is, a graph can have uni-directional or bi-directional paths (edges) between nodes. A tree has no loops, no circuits and no self-loops. Graph can have loops, circuits and also self-loops. There is only one root node and every child have only one parent. In graph there is no such concept of root node. In trees, there is parent child relationship between nodes. In Graph there is no such parent child relationship. Trees are less complex in nature then graphs. Graphs are more complex than trees as it can have cycles, loops. Types of trees are: Binary Tree , Binary Search Tree, AVL tree, Heaps. Two types of Graphs are Directed and Undirected graph. Tree always has n-1 edges. In Graph, number of edges depends on the graph. Tree is a hierarchical model. Graph is a network model. Tree is traversed in Pre-Order, In-Order and Post-Order. Graph is traversed by DFS: Depth First Search and in BFS : Breadth First Search algorithm.

Explain Heap data structure.

A heap is a specialized tree-based data structure that satisfies the heap property that If 'A' is a parent node of 'B', then the key (that is, value) of node A is ordered with respect to the key of node B with the same ordering strategy applyed across the heap.

Heap data structure is always a Binary Tree.

A heap can be of two types, max heap and min heap.

Explain max heap data structure.

The max heap is a type heap data structure where the value of the root node is greater than or equal to either of its child nodes. Explain min heap data structure.

A min-heap is a type of heap data structure where the value of the root node is less than or equal to either of its child nodes. Difference between Binary tree and Binary search tree.

A Binary tree is a type of tree data structure where each parent node can have at most two child nodes ( 0 to 2 nodes). Binary search tree is a binary tree where the left child contains only nodes with values less than the parent node, and where the right child only contains nodes with values greater than or equal to the parent node.

Define a complete Graph.

A complete graph is a graph with N vertices and an edge between every two vertices. Given that N is positive integer, there are no loops and every two vertices share exactly one edge.

The symbol KN denotes a complete graph with N vertices.

In graph theory and computer science, an adjacency matrix is a square matrix used to represent a finite graph. The elements of the matrix indicate whether pairs of vertices are adjacent or not in the graph.

Difference between undirected and Directed graph.

An undirected graph is graph with a set of vertex or nodes that are connected together, where all the edges are bidirectional. An undirected graph is sometimes called an undirected network.

In contrast, a graph where the edges point in a direction is called a directed graph.

What is a digraph in data structures?

A digraph pr directed graph is a Data Structure containing a vertex set V and an edge/arc set A, where each edge is an ordered pair of vertices. The arcs may be thought of as arrows, each one starting at one vertex and pointing at precisely one other.

Explain Adjacency list in data structures.

In graph theory and computer science, an adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a vertex in the graph.

What is Incidence matrix?

Incidence matrix is a two-dimensional Boolean matrix, in which the rows represent the vertices and columns represent the edges. The entries indicate whether the vertex at a row is incident to the edge at a column.

Incidence matrix is one of the ways to represent a graph.

What is a dense graph?

A dense graph is a graph in which the number of edges is close to the maximal number of edges.

What is a sparse graph?

Sparse graph is a graph in which the number of edges is close to the minimal number of edges.

Preferred way of representing the graph.

Adjacency lists are generally preferred because they efficiently represent sparse graphs. An adjacency matrix is preferred if the graph is dense, that is the number of edges is close to the number of vertices squared.

What is a non-simple graph?

A graph which do contain loops are Non-Simple.

Explain Knapsack problem.

The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.

What is Dynamic programming?

Dynamic programming also known as dynamic optimization is a method for solving a complex problem by breaking it down into a collection of simpler subproblems, solving each of those subproblems just once, and storing their solutions using a memory-based data structure.

The next time the same subproblem occurs, instead of recomputing its solution, one simply looks up the previously computed solution, thereby saving computation time at the expense of a modest expenditure in storage space. Each of the subproblem solutions is indexed in some way, typically based on the values of its input parameters, so as to facilitate its lookup. The technique of storing solutions to subproblems instead of recomputing them is called memoization.

Difference between memoization and dynamic programming.

Memoization is a term describing an optimization technique where you cache previously computed results, and return the cached result when the same computation is needed again.

Dynamic programming is a technique for solving problems recursively and is applicable when the computations of the subproblems overlap.

Dynamic programming is typically implemented using tabulation, but can also be implemented using memoization.

How insertion sort is faster than bubble sort?

In bubble sort at i-th iteration you have n-i-1 inner iterations ((n^2)/2), but in insertion sort you have maximum i iterations on i-th step, but i/2 on average, as you can stop inner loop earlier, after you found correct position for the current element. So you have (sum from 0 to n) / 2 which is (n^2) / 4.

Explain trie data structure.

Trie is an ordered tree data structure that is used to store a dynamic set or associative array where the keys are usually strings. Unlike a binary search tree, no node in the tree stores the key associated with that node; instead, its position in the tree defines the key with which it is associated.

An trie is also called digital tree, radix tree or prefix tree.

What is edge case in algorithm evaluation?

Edge cases are inputs that the specification for your function allows but that might be tricky to handle.

An edge case is a problem or situation that occurs only at an extreme (maximum or minimum) operating parameter. For example, a stereo speaker might noticeably distort audio when played at its maximum rated volume, even in the absence of other extreme settings or conditions.

How to represent complexity when the best case, average case, and worst case are equal?

Use Theta to represent complexity when best/average/worst case are equal.

How do I delete an element from heap data structure?

Deletion always occurs at the root of the heap. The root element is removed and it is replaced by the last element in the heap. This satisfies the shape property, however, it violates the order property as the last element would be the smallest(in case of the max heap). The Reheapify operation needs to be performed to satisfy the order property.

What is depth of node in a tree.

Depth is the node is equal to the number of edges to the node from root node. The depth of root node is 0.

Also maximum number of children at level i is 2 to the power of I.

How to find the height of a node in a tree?

The height of the node is equal to the number of edges in the longest path to the leaf from the node. The depth of a leaf node is 0.

Height of the tree is nothing but the height of the root node.

What is a strict or proper binary tree?

In a strict binary tree, each node can have either 0 or 2 children.

Define complete and perfect binary tree.

In complete binary tree, all level except the last are completely filled and all the node are on left as possible and no vacant space on left side.

In perfect binary tree, all levels are completely filled.

Difference between Insertion Sort and Selection Sort.

Both insertion sort and selection sort have nested loops, one outer and inner loop.

In selection sort, the inner loop is over the unsorted elements. Each pass selects one element and moves it to its final location which is at the current end of the sorted region. In insertion sort, each pass of the inner loop iterates over the sorted elements. Sorted elements are displaced until the loop finds the correct place to insert the next unsorted element.

What is the complexity of Merge sort algorithm?

Merge sort takes O (n log n) in terms of worst case scenario.

What is inverted binary tree?

Inverted binary tree is the mirror image of the tree where the left of the nodes move to right and vice versa.

Explain jagged array.

A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is also called as array of arrays.