(DSA) 1 & 2 Marks Important Questions With Answers Data Structure & Algorithm
1 & 2 Marks (IMP)
1. Define data structure. List out its types.
A data structure is a way of organizing and storing data in a computer so that it can be accessed and modified efficiently.
Types:
Primitive data structures: (int, float, char, double, bool)
Non-primitive data structures (Linear and Non-linear)
2. Define pointer. How to initialize pointer.
A pointer is a variable that stores the memory address of another variable.
Initialization:
int a = 10;
int *p = &a;
3. List out primitive and non-primitive data structures.
Primitive: int, float, char, double, boolean
Non-primitive: Arrays, Linked Lists, Stacks, Queues, Trees, Graphs
4. What is pointer topointer how to declare it.
A pointer to pointer is a variable that stores the address of another pointer variable.
Declaration:
int **ptr2ptr;
5. What is dynamic and static memory allocation.
Static Memory Allocation: Memory is allocated at compile-time (e.g., arrays).
Dynamic Memory Allocation: Memory is allocated at runtime (e.g., using
malloc
,calloc
).
6. What are primitive and non-primitive data structures? Give examples.
Primitive: Basic data types like
int
,char
,float
(e.g.,int a = 5;
)Non-primitive: Derived structures like
arrays
,linked lists
,trees
(e.g.,int arr[10];
)
7. List advantages of pointer.
Efficient memory management
Dynamic memory allocation
Enables creation of complex data structures (like linked lists)
Used for function arguments by reference
8. State the use of calloc( ) function with syntax.
Allocates multiple blocks of memory and initializes them to zero.
Syntax:
ptr = (int*) calloc(n, sizeof(int));
9. List applications of data structures.
Compiler design
Operating systems
Database management systems
Artificial Intelligence
Network data packet organization
File systems
10. What is recursion. Give an example.
Recursion is a programming technique where a function calls itself directly or indirectly to solve a problem by breaking it into smaller subproblems.
Example (Factorial using Recursion):
int factorial(int n)
{ if (n == 0) return 1; else return n * factorial(n - 1); }
11. Differentiate linear search and binary search.
Linear Search | Binary Search |
Sequentially checks each element. | Divides the list into halves for searching. |
O(n) (Worst case) | O(log n) (Faster) |
Works on any list (sorted/unsorted). | Requires a sorted list. |
Brute-force. | Divide and Conquer. |
12. Define searching and sorting.
Searching: It is the process of finding a specific element in a collection of data. or Searching is the process of finding the location or existence of a particular element or value within a data structure (like an array, list, or tree).
Example Algorithms: Linear Search, Binary Search.
Sorting: It is the process of arranging data in a particular order (ascending or descending). or Sorting is the process of arranging data in a specific order, typically in ascending or descending order.
Example Algorithms: Bubble Sort, Selection Sort, Insertion Sort.
13. What is the difference between bubble sort, selection sort and insertion sort.
- Bubble Sort: Repeatedly swaps adjacent elements if they are in wrong.
- Selection Sort: Finds the smallest/largest element and places it in correct position
- Insertion Sort: Inserts each element into its correct position in a sorted part
14. What is stack? List out the operations on stack.
A stack is a linear data structure that follows the Last In First Out (LIFO) principle, meaning the last element added is the first one to be removed.
Operations on Stack:
Push() – Inserts an element at the top.
Pop() – Removes and returns the top element.
Peek() / Top() – Returns the top element without removing it.
isEmpty() – Checks if the stack is empty.
isFull() – Checks if the stack is full (for fixed-size stacks).
15. Convert the given expression into postfix expression
i. A / B * (C + D / E) – F
Postfix: A B / C D E / + * F -
ii. (A + B) / (C – D)
Postfix: A B + C D - /
16. Expand LIFO and FIFO.
LIFO → Last In First Out (Used in Stacks)
FIFO → First In First Out (Used in Queues)
17. What are push() and pop() operations.
push()
– adds an element to the top of the stackpop()
– removes the top element from the stack
18. What is LIFO? List applications of it.
LIFO (Last In First Out): The last element added to a stack is the first one to be removed.
Applications:
Expression evaluation
Syntax parsing
Undo mechanisms in editors
Backtracking (e.g., maze solving)
19. What are the drawbacks of ordinary queue?
Wasted space due to front-end deletions
Cannot reuse space without shifting elements
Inefficient memory usage in static implementation
20. What is priority queue? Mention different types of it.
A priority queue is a data structure where each element is assigned a priority, and elements are served based on priority.
Types:
Max Priority Queue
Min Priority Queue
Double-Ended Priority Queue
A priority queue is a queue where elements are dequeued based on priority (highest/lowest first).
Types of Priority Queues:
Ascending Priority Queue → Smallest element removed first.
Descending Priority Queue → Largest element removed first.
21. Evaluate the following postfix expression 12-34-*
Step-by-step: (Solve Your Own)
12 -
→ Result:-1
34 -
→ Result:-1
-1 * -1
→ Result:1
Answer: 1
22. What is circular queue?
A circular queue is a linear data structure where the last position is connected back to the first to make a circle. It overcomes the space limitation of a normal queue by reusing empty slots after deletion.
Advantages:
- Efficient Space Utilization (Reuses empty spaces after deletion).
- Avoids Wastage (No fixed front limitation like in linear queues).
23. What is Linked list? Mention the operations on list.
A linked list is a linear data structure where each element (node) points to the next node in the sequence.
Operations:
Insertion
Deletion
Traversal
Searching
Updating
24. What is singly linked list? How do you declare it.
A singly linked list is a list where each node points to the next node and the last node points to NULL
.
Declaration in C:
struct Node
{
int data;
struct Node* next;
};
25. What is Node? Classify linked list.
A node is a basic unit of a linked list containing data and a pointer to the next (or previous) node.
Types of Linked Lists:
Singly Linked List
Doubly Linked List
Circular Linked List
Doubly Circular Linked List
26. Differentiate singly linked list and doubly linked list.
Singly Linked List | Doubly Linked List |
Unidirectional (next only). | Bidirectional (prev and next). |
Less memory (1 pointer per node). | More memory (2 pointers per node). |
Forward only. | Forward & backward traversal. |
Harder (requires previous node). | Easier (direct prev access). |
27. Define Tree. Mention the basic operations on a Tree.
A tree is a non-linear hierarchical data structure consisting of nodes, with a single root node and subtrees of children.
Operations:
Insertion
Deletion
Traversal (Inorder, Preorder, Postorder)
Searching
28. Define degree and depth of a tree.
Degree of a Tree (or Node): The degree of a node in a tree is the number of children it has. The degree of the tree is the maximum degree of any node in the tree.
- Example: If a node has 3 children, its degree is 3. If the highest number of children any node has in the entire tree is 3, the tree’s degree is 3.
Depth of a Node: The depth of a node is the number of edges from the root to that node.
The root node has a depth of 0.
Each level down increases the depth by 1.
Example:
A ← Depth 0 / \ B C ← Depth 1 / \ D E ← Depth 2
29. What are terminal and non-terminal nodes?
Terminal Nodes (Leaf Nodes): These are nodes in a tree that do not have any children. In other words, they are at the bottom of the tree and represent the end of a branch.
Example: In a binary tree, a node with both left and right pointers asNULL
is a terminal node.Non-Terminal Nodes (Internal Nodes): These are nodes that have at least one child. They act as intermediate points in the tree structure and are not leaves.
Example: The root node and any node that connects to child nodes are non-terminal.
Example:
A ← (Root) / \ B C ← (Non-Terminal) / \ D E ← (Terminal/Leaf)
30. What is binary search tree?
A Binary Search Tree is a special type of binary tree where:
The left child of a node contains values less than the node.
The right child contains values greater than the node.
Example:
40
/ \
30 50
31. Define strict binary tree. Give examples.
A Strict Binary Tree (also called Proper or Full Binary Tree) is a binary tree where every node has either 0 or 2 children—no node has only one child.
Example: (All non-leaf nodes have exactly 2 children).
A
/ \
B C
32. Define complete binary tree.
A Complete Binary Tree is a type of binary tree in which all levels are completely filled, except possibly the last level, and in the last level, all nodes are as far left as possible.
Example:
1
/ \
2 3
/\
4 5
33. What are siblings of a tree?
Siblings are nodes that share the same parent in a tree.
Example:
A
/ \
B C
Here, B and C are siblings because they both have the same parent node A.
34. Define path of tree.
A path in a tree is a sequence of connected nodes starting from one node and moving along the edges to another node.
A path can go from the root to any node, or from one node to any of its descendants.
The length of a path is the number of edges between the starting and ending nodes.
Example:
A
/ \
B C
/
D
35. What is a queue? list out the types of queues.
A queue is a linear data structure that follows the FIFO (First In, First Out) principle. This means that the element inserted first is the one to be removed first.
Types:
Simple Queue (Linear Queue)
Circular Queue
Priority Queue
Double-Ended Queue (Deque)
Blocking Queue
Bounded Queue
Unbounded Queue
36. What are queue applications?
- CPU scheduling
- Print job management
- Call center systems
- Breadth-First Search (BFS) in graphs
37. What is a Stack? List out Operations of a Stack.
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. This means the last element added to the stack is the first one to be removed.
Operations of a Stack:
Push – Add an element to the top of the stack.
Pop – Remove the element from the top of the stack.
Peek/Top – View the element at the top without removing it.
IsEmpty – Check if the stack is empty.
IsFull – (In array-based stacks) Check if the stack is full.
38. Define pointers Give syntax and example
A pointer is a variable that stores the memory address of another variable.
Syntax:
datatype *pointer_name;
Example:
int x = 10; int *ptr = &x; printf("%d", *ptr);
39. What is data in data structure?
- Data means the values or information that we store in a computer.
- In data structures, data is the basic element that we organize and manage using structures like arrays, stacks, and queues.
40. Define priority queue. Give its types.
A priority queue is a special type of queue where each element has a priority, and elements with higher priority are served first.
Types of Priority Queues:
Max Priority Queue
Min Priority Queue
Double – Ended Priority Queue (Deap)
41. List some applications of stack.
A stack is a linear data structure that follows LIFO (Last In, First Out).
Function call management (call stack)
Undo/redo in editors
Expression evaluation (postfix, infix)
Syntax parsing
Backtracking (mazes, puzzles)
42. Define true and degree of a node.
- Tree: A tree is a hierarchical data structure made up of nodes connected by edges. It starts from a special node called the root, and each node can have zero or more child nodes.
- Degree of a Node: The degree of a node is the number of children it has.
Example: If node A has 2 children, its degree is 2.
The degree of a tree is the maximum degree among all its nodes.
43. What is sequential search with its advantages & Example.
Sequential search (or linear search) scans each element of a list one by one until it finds the target or reaches the end.
Advantages:
Simple to implement
Works on unsorted data
No additional memory needed
Example:
for(i = 0; i < n; i++)
{
if(arr[i] == key)
return i;
}
44. Define structure ? Write the syntax of structure.
- A structure is a user-defined data type in C/C++ that allows you to group variables of different data types under a single name.
- It is used to represent a record or object with multiple properties.
Syntax:
struct StructureName
{
dataType member1;
dataType member2;
// ... other members
};
45. Mention the types of dynamic memory allocations.
malloc()
calloc()
realloc()
free()
46. Mention any two advantages of Doubly Linked List.
Traversal is possible in both directions (forward and backward).
Easier deletion of a node when compared to singly linked lists.
47. Differentiate singly and doubly linked list.
Singly Linked List | Doubly Linked List |
---|---|
Has one pointer (next). | Has two pointers (next and prev). |
Moves in one direction. | Moves in both directions. |
Uses less memory. | Uses more memory. |
Can’t go backward. | Can go forward and backward. |
Easier to create. | Slightly harder to create. |
48. How to Declare a File Pointer with example?
A file pointer is used to handle files. It is declared using the FILE
type provided by the standard library.
Syntax:
FILE *fp;
Example:
FILE *fp;
fp = fopen("example.txt", "r"); // Open file in read mode
if (fp == NULL)
{
printf("Error opening file.\n");
}