Data Structures & Algorithms (PCC-CS301) question and answers:
Data structure: A way to store and organize data to perform operations efficiently.
Array vs. linked list: Arrays are fixed-size, contiguous memory structures; linked lists are dynamic, with nodes linked by pointers.
Stack: A Last In, First Out (LIFO) structure with
push
andpop
operations.Queue: A First In, First Out (FIFO) structure, with
enqueue
to add anddequeue
to remove elements.Types of linked lists: Singly linked list, doubly linked list, and circular linked list.
Binary tree: A tree with each node having up to two children, left and right.
Binary search tree (BST): A binary tree where the left child is less than the parent and the right child is greater.
DFS vs. BFS: Depth-First Search explores as far as possible along branches, while Breadth-First Search explores all neighbors before moving deeper.
Heap: A specialized binary tree that satisfies the heap property, used in priority queues and heapsort.
Hash table: Stores key-value pairs for efficient lookup using a hashing function.
Graph: A collection of nodes (vertices) connected by edges, represented by adjacency lists or matrices.
Pointer: A variable holding the memory address of another variable, commonly used in dynamic structures.
Types of sorting algorithms: Quick sort, merge sort, bubble sort, insertion sort, selection sort, heap sort, etc.
Quicksort: A divide-and-conquer algorithm that partitions an array around a pivot, sorting in
O(n log n)
on average.Merge sort: A stable sorting algorithm that recursively divides and merges arrays, with
O(n log n)
complexity.Bubble sort: Repeatedly swaps adjacent elements if out of order; time complexity is
O(n^2)
.Insertion sort: Builds the sorted array one item at a time, best for small data or partially sorted arrays.
Selection sort: Finds the minimum element and places it at the beginning, with
O(n^2)
complexity.Recursion: A function calling itself to solve a problem in smaller subproblems.
Dynamic programming: Optimizes by storing overlapping subproblem results to avoid recomputation.
Greedy algorithm: Makes the locally optimal choice at each step, aiming for a global optimum.
Backtracking: Explores all possible solutions by building a solution incrementally and abandoning it if it fails.
Binary search: Efficiently finds elements in a sorted array by repeatedly dividing the search interval in half.
Time complexities:
- Array access:
O(1)
, insertion/deletion:O(n)
- Linked list access:
O(n)
, insertion/deletion:O(1)
- Stack/Queue:
O(1)
for push/pop/enqueue/dequeue.
- Array access:
Balanced binary tree: Maintains near-equal heights in subtrees, improving efficiency of operations.
AVL tree: A self-balancing binary search tree where the height difference between subtrees is ≤1.
Red-black tree: A balanced BST with properties that enforce a near-equal path length to leaves.
Graph traversal: Visiting nodes in a graph, typically through DFS or BFS.
Adjacency matrix vs. list:
- Matrix: 2D array,
O(V^2)
space, fast access. - List: Stores edges only,
O(E)
space, space-efficient for sparse graphs.
- Matrix: 2D array,
Dijkstra's algorithm: Finds shortest path from a source to all other nodes in a weighted graph.
0 Comments