Viva Das
Viva Das
Arrays
What is an array, and how is it stored in memory?
An array is a collection of elements of the same data type stored in contiguous memory locations. It allows indexed access to elements.
How do you access the elements of an array?
Elements in an array can be accessed using their index. For example, arr[i] accesses the element at the ith position.
Explain the difference between static and dynamic arrays.
Static arrays have a fixed size determined at compile-time. Dynamic arrays can change size at runtime, typically managed with functions like malloc in C.
What are the advantages and disadvantages of arrays?
Advantages: Simple indexing, efficient memory access. Disadvantages: Fixed size (in static arrays), inserting/deleting is costly.
How do you find the largest or smallest element in an array?
Traverse the array, compare each element with a variable storing the largest or smallest value, and update accordingly.
How do you reverse an array?
Swap elements from the start and end of the array, moving towards the center.
Explain how to remove duplicates from a sorted array.
Traverse the array and skip any elements that are the same as the last unique element.
How do you implement binary search in an array?
Perform binary search by comparing the middle element to the target and adjusting search bounds accordingly (requires sorted array).
What is a multidimensional array, and how is it different from a regular array?
A multidimensional array is an array of arrays, enabling structures like matrices (2D arrays). Each element can be accessed with multiple indices.
Write a program to rotate an array by k elements.
Shift each element to the right or left by k positions, either through rotations or using array slicing.
Linked Lists
What is a linked list, and how does it differ from an array?
A linked list consists of nodes where each node points to the next. Unlike arrays, linked lists do not have fixed sizes and allow for dynamic memory allocation.
Explain the types of linked lists.
Singly linked list (one pointer per node), Doubly linked list (two pointers, one for next and one for previous), Circular linked list (last node points back to the first).
How do you insert a node at the beginning/middle/end of a linked list?
Create a new node, adjust pointers of adjacent nodes, and link the new node accordingly.
How can you delete a node from a linked list?
Locate the node, adjust the pointers of adjacent nodes to bypass it, and free the node’s memory.
What are the advantages of using a doubly linked list over a singly linked list?
Doubly linked lists allow bidirectional traversal, which makes deletion and backward traversal easier.
Write a program to reverse a linked list.
Change the direction of each pointer in the list by iterating through each node and updating pointers accordingly.
How do you detect a loop in a linked list?
Use Floyd’s Cycle Detection Algorithm (slow and fast pointer approach) to check if a loop exists.
What is the difference between a circular linked list and a regular linked list?
In a circular linked list, the last node points back to the first node, creating a loop.
How do you find the middle element in a linked list?
Use the slow and fast pointer method where the fast pointer moves two steps for every one step of the slow pointer.
Write a program to merge two sorted linked lists.
Traverse both lists, and merge them by linking the smaller node in each step.
Stack
What is a stack, and what are its applications?
A stack is a LIFO (Last In, First Out) data structure used in applications like function call management, expression evaluation, and undo operations.
Explain the LIFO (Last In, First Out) principle with an example.
In a stack, the most recently added element is removed first. Example: Pushing numbers 1, 2, and 3 results in 3 being popped first.
What is stack overflow and stack underflow?
Stack overflow occurs when pushing to a full stack, while underflow happens when popping from an empty stack.
How do you implement a stack using an array and a linked list?
Array: Use an array with a pointer tracking the top element. Linked list: Use nodes with a pointer indicating the top node.
Write a program for infix to postfix expression conversion using stack.
Traverse the infix expression, using precedence rules to manage operators and parentheses with a stack.
Explain how to check if a string has balanced parentheses.
Use a stack to push opening brackets and pop on encountering closing brackets, ensuring each match is balanced.
What is the use of stacks in function calls and recursion?
Stacks manage local variables, return addresses, and control flow during recursive calls.
Write a program to evaluate a postfix expression using a stack.
Push operands to the stack, and for each operator, pop the required operands, apply the operator, and push the result back.
What are the limitations of stack in terms of memory?
Stack size is limited by memory constraints, which can lead to overflow during deep recursions or large data pushes.
How can you implement two stacks in a single array?
Use one stack from the start and another from the end, allowing them to grow towards each other.
Queue
What is a queue, and what are its applications?
A queue is a FIFO (First In, First Out) structure used in scheduling tasks, managing resources, and handling requests.
Explain the FIFO (First In, First Out) principle with an example.
The first item added is the first to be removed. Example: Enqueuing numbers 1, 2, and 3 results in 1 being dequeued first.
What is the difference between a queue and a stack?
Queue follows FIFO, while stack follows LIFO.
How do you implement a queue using an array and a linked list?
Array: Use front and rear pointers. Linked list: Use nodes with front and rear pointers indicating queue endpoints.
Explain circular queues and how they differ from regular queues.
Circular queues treat the array as circular, wrapping around the end, which optimizes space.
Write a program to implement a circular queue.
Maintain front and rear pointers and handle overflow by wrapping the rear pointer to the start.
What is a priority queue, and how is it implemented?
Priority queue processes elements based on priority, often implemented using heaps.
How do you handle queue overflow and underflow?
Overflow happens if a new element cannot be added; underflow occurs if a queue is empty during dequeue.
What are the differences between a dequeue and a regular queue?
Dequeue (double-ended queue) allows insertion and deletion from both ends.
How can you implement a queue using two stacks?
Use two stacks: one for enqueueing and another for dequeueing by reversing the order of elements.
Tree
What is a binary tree, and how does it differ from a binary search tree?
A binary tree has at most two children, while a binary search tree (BST) is ordered such that the left child < root < right child.
Explain the types of binary trees (full, complete, balanced).
Full: Every node has 0 or 2 children.
Complete: Every level, except possibly the last, is fully filled.
Balanced: Heights of left and right subtrees differ by at most 1.
How do you perform in-order, pre-order, and post-order traversal on a binary tree?
In-order: Left -> Root -> Right
Pre-order: Root -> Left -> Right
Post-order: Left -> Right -> Root
What is a balanced tree, and why is it important?
A balanced tree keeps operations efficient, typically ensuring a height of O(log n).
Explain the concept of height and depth in a tree.
Height: Max number of edges from a node to the furthest leaf. Depth: Number of edges from the root to a node.
How do you implement a binary search tree (BST) in C?
Insert nodes according to BST property, placing values less than root to the left, and greater to the right.
How can you delete a node in a binary search tree?
Leaf node: Delete directly.
One child: Replace with child.
Two children: Replace with in-order successor or predecessor.
What is the difference between BFS and DFS traversal?
BFS explores nodes level by level.
DFS explores depth first, using pre-order, in-order, or post-order traversal.
Write a program to find the lowest common ancestor in a binary tree.
Traverse tree, comparing values with nodes; recurse until LCA is found.
Explain AVL and Red-Black Trees.
Self-balancing BSTs where AVL strictly balances heights, and Red-Black Trees use color properties to balance less strictly.
Graph
What is a graph, and what are its types?
A graph is a collection of nodes (vertices) and edges. Types: Directed, undirected, weighted, and unweighted.
Explain the difference between directed and undirected graphs.
Directed: Edges have a direction from one vertex to another.
Undirected: Edges have no direction.
What is an adjacency matrix and adjacency list?
Adjacency matrix: 2D array representing edges.
Adjacency list: Array of lists where each list represents adjacent nodes.
How do you represent a graph in C using adjacency matrix/list?
Matrix: 2D array of 1s and 0s for edges.
List: Array of linked lists or arrays.
What is BFS (Breadth-First Search), and how is it implemented?
Uses a queue to explore nodes level by level.
What is DFS (Depth-First Search), and how is it implemented?
Uses recursion or stack to explore nodes in depth.
Explain the concept of connected components in a graph.
A set of vertices in a graph that are connected to each other.
How do you detect a cycle in a graph?
Use DFS and mark nodes visited, or track back edges.
What is Dijkstra’s algorithm? How is it used for finding the shortest path?
A greedy algorithm that finds the shortest path from a source node to all other nodes in a weighted graph.
What are spanning trees and minimum spanning trees?
A spanning tree is a subset of a graph that connects all vertices with the minimum number of edges. Minimum spanning trees have the smallest possible edge weights.