Dsa Theory
Dsa Theory
A: Time complexity represents how the runtime of an algorithm increases with input size. It is
important because it helps analyze the efficiency of an algorithm and compare different
solutions.
A:
Q3: Compare O(n log n) and O(n²). When does one perform better than the
other?
A: O(n log n) (e.g., MergeSort) is better than O(n²) (e.g., BubbleSort) for large inputs. O(n²) is
only preferable for small datasets due to lower constant factors.
A: Use recurrence relations and solve them using the Master Theorem or recursion tree
method.
A:
A:
A:
3. Linked Lists
Q8: Compare singly linked lists and doubly linked lists.
A:
Q9: How can you detect and remove a cycle in a linked list?
A: Use Floyd’s Cycle Detection Algorithm (Tortoise & Hare method) to detect the cycle,
then remove it by adjusting pointers.
A: A stack is a LIFO (Last In, First Out) data structure used in function calls, expression
evaluation, and backtracking (e.g., DFS).
A: A circular queue reuses empty space by wrapping around when the end is reached,
whereas a normal queue does not.
5. Trees & Graphs
Q12: What is a binary search tree (BST)?
A: A BST is a binary tree where the left child is smaller than the parent, and the right child is
greater.
A:
A:
Dijkstra’s algorithm finds the shortest path from a source node to all other nodes in a graph
using a priority queue (min heap).
A: Merge Sort is preferred because it does not require random access and works well with
linked list pointer manipulations.
A:
● Stable Sort: Maintains the relative order of duplicate elements (e.g., Merge Sort).
● Unstable Sort: Does not guarantee order (e.g., QuickSort).
7. Hashing
Q17: What is a hash function?
A: A function that converts an input (key) into a fixed-size numerical value (hash code).
Q18: How do hash collisions occur, and how can they be resolved?
A:
Collisions occur when two keys generate the same hash. They can be resolved using:
Q20: Why is the 0/1 Knapsack problem solved using DP instead of greedy?
A: The greedy approach fails because it doesn’t always give the optimal solution. DP
considers all combinations.
9. Bit Manipulation
Q21: How do you check if a number is a power of 2 using bitwise
operations?
A:
boolean isPowerOfTwo(int n) {
return (n > 0) && (n & (n - 1)) == 0;
}
A: A trie is a tree-based data structure used for efficient string searching (e.g., auto-complete,
spell checking).
I am a quick learner. If I am given a task I'll make sure I complete it with full efficiency. There
might be a situation where i might not be aware of some topics or things given to me as a task
but i ll still make sure i research and learn about it to complete it in time,
Tab 3
1. which is better merge sort/quick sort ?
If you need stable sorting or are working with linked lists → Use Merge Sort.
If you need fast in-place sorting and have limited memory → Use Quick Sort.
For large datasets, Quick Sort is usually better due to its lower constant factors.
3. What is the estimated value of 2^24 (give a mental roadmap of how you arrived at its
value, hint: 2^24 = 2^4*(2^10)^2 = 16*(10^3)^2 = 16 million)
Squaring 2102^{10}210:
(210)2=(1000)2=106=1,000,000(2^{10})^2 = (1000)^2 = 10^6 =
1,000,000(210)2=(1000)2=106=1,000,000
Multiplying by 242^424:
224=16×1,000,0002^{24} = 16 \times 1,000,000224=16×1,000,000
Final Answer:
16,000,000 (16 million)16,000,000 \text{ (16 million)}16,000,000 (16 million)
4. Worst case complexity of look up time in self balancing BST? What about in normal
BST?
● Why?
○ If the BST becomes skewed (like a linked list), each lookup may require traversing
all n nodes.
Why?
○ If all keys hash to the same bucket (worst-case collision), forming a linked list or
tree at that bucket.
○ In open addressing, if the table is almost full, probing for an empty slot can take
O(n).
○ In rehashing, when the table is resized, reinserting all elements takes O(n).
Why?
With a good hash function and low load factor (e.g., α<0.7\alpha < 0.7α<0.7), collisions
are rare.
a. Most insertions happen in constant time by simply placing the key in an empty
bucket.
6. Examples of self balancing BST
● If the array has more than one element, divide it into two halves.
● Recursively apply merge sort on each half.
Priority queue is implemented using min/max heap which uses a binary tree data structures.
9. For an unweighted graph, which algorithm should be used to find shortest path:
breadth first search or Dijkstras?
For an unweighted graph, you should use Breadth-First Search (BFS) to find the shortest path.
🚀
Why BFS?
✅ Correctness: In an unweighted graph, the shortest path is the one with the fewest edges. BFS
explores all nodes level by level, ensuring that when it reaches a node, it has found the shortest
path to it.
✅ Time Complexity:
● BFS: O(V+E)O(V + E)O(V+E) (where VVV = vertices, EEE = edges)
● Dijkstra’s: O((V+E)logV)O((V + E) \log V)O((V+E)logV) (because of the priority queue)
● BFS is faster for an unweighted graph since it doesn’t require priority queues or extra
overhead.
❌ Unnecessary Complexity:
● Dijkstra’s Algorithm is designed for weighted graphs. It uses a priority queue to always
expand the minimum cost node, but in an unweighted graph, all edge costs are equal (1),
making this unnecessary.
● BFS naturally finds the shortest path without extra computational overhead.
JAVA
In Java, a reference is a variable that stores the memory address of an object rather than the
actual object itself.
Key Points: