DSA Interview Patterns Interview Questions Hindi mein

DSA Interview Patterns कोर्स के 26 सवाल, हर सवाल के सही जवाब और explanation के साथ। जवाब देखने से पहले ख़ुद सोचें।

How to Approach Coding Interviews

1. What should you do before writing code?

  1. Ask for the answer
  2. Start typing the optimal solution immediately
  3. Clarify constraints and work through examples
  4. Write tests for every possible input
जवाब देखें

जवाब: Clarify constraints and work through examples

Understanding first.

पाठ: A Repeatable Problem-Solving Method

2. With n up to 100,000, which complexity is usually acceptable?

  1. O(n log n)
  2. O(n²)
  3. O(2ⁿ)
  4. O(n!)
जवाब देखें

जवाब: O(n log n)

10^10 operations is too slow.

पाठ: Big-O and Why It Matters

3. Which pattern does "longest substring with at most k distinct characters" suggest?

  1. Backtracking
  2. Topological sort
  3. Dijkstra
  4. Sliding window
जवाब देखें

जवाब: Sliding window

Contiguous plus longest.

पाठ: Recognising Patterns

Arrays and Hashing

4. What is the average lookup time in a hash map?

  1. O(n)
  2. O(1)
  3. O(log n)
  4. O(n²)
जवाब देखें

जवाब: O(1)

Constant on average.

पाठ: Hash Map Lookups

5. Why use prefix sums instead of a sliding window for "subarray sum equals k"?

  1. They use no memory
  2. They work even when the array contains negative numbers
  3. They sort the array
  4. They are always O(1) overall
जवाब देखें

जवाब: They work even when the array contains negative numbers

Windows need monotonic sums.

पाठ: Prefix Sums

6. What makes a good key for grouping anagrams?

  1. A random number
  2. The word length only
  3. The first letter
  4. The sorted letters of each word
जवाब देखें

जवाब: The sorted letters of each word

Equivalent items share a key.

पाठ: Counting and Canonical Keys

Two Pointers and Sliding Windows

7. In "container with most water", which pointer should move?

  1. The one at the shorter line
  2. The one at the taller line
  3. Both always
  4. Neither; use nested loops
जवाब देखें

जवाब: The one at the shorter line

The shorter side limits the area.

पाठ: Two Pointers

8. Why is a variable sliding window O(n) despite having a nested shrink step?

  1. Python optimises it
  2. The inner loop never runs
  3. Each index is added and removed at most once
  4. Because the input is sorted
जवाब देखें

जवाब: Each index is added and removed at most once

Amortised analysis.

पाठ: Sliding Windows

9. Why can smaller values be removed from the back of the deque?

  1. The deque has a size limit
  2. They are duplicates
  3. A newer, larger value will outlast them in every future window
  4. They are negative
जवाब देखें

जवाब: A newer, larger value will outlast them in every future window

They can never be the maximum.

पाठ: Monotonic Deques

Binary Search and Stacks

10. What does lower bound return for a target larger than all elements?

  1. The length of the array
  2. -1
  3. 0
  4. The last index
जवाब देखें

जवाब: The length of the array

It is the insertion point.

पाठ: Binary Search Done Right

11. What property makes binary search on the answer valid?

  1. The array has no duplicates
  2. The input is sorted
  3. The answer is always an even number
  4. Feasibility is monotonic in the answer
जवाब देखें

जवाब: Feasibility is monotonic in the answer

Once feasible, always feasible above.

पाठ: Binary Search on the Answer

12. Which pattern finds the next greater element for every position in O(n)?

  1. A min-heap
  2. A monotonic stack
  3. Binary search on each element
  4. Sorting the array
जवाब देखें

जवाब: A monotonic stack

Pending items resolve together.

पाठ: Stacks and Monotonic Stacks

Linked Lists and Trees

13. How does Floyd's algorithm detect a cycle?

  1. By storing every node in a list
  2. A fast pointer moving two steps eventually meets a slow pointer moving one step
  3. By sorting the list
  4. By counting to a fixed limit
जवाब देखें

जवाब: A fast pointer moving two steps eventually meets a slow pointer moving one step

O(1) space cycle detection.

पाठ: Fast and Slow Pointers

14. Why must BST validation pass bounds down the tree?

  1. To count nodes
  2. To make it faster
  3. Because recursion requires parameters
  4. A node must respect all ancestors, not just its parent
जवाब देखें

जवाब: A node must respect all ancestors, not just its parent

Local checks miss deep violations.

पाठ: Tree Depth-First Search

15. Which data structure drives BFS?

  1. A queue
  2. A stack
  3. A heap
  4. A hash set only
जवाब देखें

जवाब: A queue

First in, first out.

पाठ: Tree Breadth-First Search

Graphs

16. Why does BFS find shortest paths in unweighted graphs?

  1. It uses a priority queue
  2. It explores randomly
  3. It explores nodes in increasing order of distance
  4. It visits deepest nodes first
जवाब देखें

जवाब: It explores nodes in increasing order of distance

Layer by layer.

पाठ: BFS for Shortest Paths

17. What does a failed union (both nodes already share a root) indicate in an undirected graph?

  1. The nodes are disconnected
  2. The graph is empty
  3. The edge would create a cycle
  4. The algorithm has a bug
जवाब देखें

जवाब: The edge would create a cycle

They were already connected.

पाठ: Connected Components and Union-Find

18. How does Kahn's algorithm detect a cycle?

  1. Not all nodes get processed because some never reach in-degree zero
  2. The queue overflows
  3. A node is visited twice
  4. The graph has more edges than nodes
जवाब देखें

जवाब: Not all nodes get processed because some never reach in-degree zero

Cycles keep in-degrees positive.

पाठ: Topological Sort

19. When does Dijkstra's algorithm fail?

  1. When there are more than 100 nodes
  2. When the graph is directed
  3. When weights are integers
  4. When the graph has negative edge weights
जवाब देखें

जवाब: When the graph has negative edge weights

It assumes settled distances never improve.

पाठ: Weighted Shortest Paths

Heaps, Intervals and Greedy

20. What does a min-heap of size k contain after scanning an array for the k largest?

  1. The k smallest elements
  2. The k largest elements, with the k-th largest at the root
  3. All elements sorted
  4. Only the maximum
जवाब देखें

जवाब: The k largest elements, with the k-th largest at the root

Pop the smallest when over k.

पाठ: Heaps for Top-k

21. What is the usual first step in interval problems?

  1. Reverse the list
  2. Sort intervals by start time
  3. Convert to a graph
  4. Use binary search immediately
जवाब देखें

जवाब: Sort intervals by start time

Order makes a single sweep possible.

पाठ: Interval Problems

22. When is a greedy algorithm correct?

  1. Only when n is small
  2. Always
  3. Only for sorted inputs
  4. When a locally optimal choice can be shown to lead to a global optimum
जवाब देखें

जवाब: When a locally optimal choice can be shown to lead to a global optimum

Needs a proof or exchange argument.

पाठ: Greedy Algorithms

Dynamic Programming and Backtracking

23. Why does greedy fail for coin change with coins [1, 3, 4] and amount 6?

  1. Taking 4 first leads to 4+1+1 (3 coins) while 3+3 needs only 2
  2. Greedy always succeeds
  3. Amount 6 cannot be made
  4. Coins must be sorted descending
जवाब देखें

जवाब: Taking 4 first leads to 4+1+1 (3 coins) while 3+3 needs only 2

DP considers all choices.

पाठ: One-Dimensional DP

24. What does dp[i][j] mean in edit distance?

  1. The length of a
  2. The number of matching characters
  3. The minimum edits to turn the first i characters of a into the first j of b
  4. The index of the first mismatch
जवाब देखें

जवाब: The minimum edits to turn the first i characters of a into the first j of b

Answers for prefixes.

पाठ: Two-Dimensional DP

25. What is the "unchoose" step in backtracking?

  1. Sorting the results
  2. Deleting the input
  3. Undoing the last choice after exploring it
  4. Returning early always
जवाब देखें

जवाब: Undoing the last choice after exploring it

Restore state for the next option.

पाठ: Backtracking

26. What should you do right after finishing the code?

  1. Trace it on an example and test edge cases
  2. Immediately say it is done
  3. Rewrite it in another language
  4. Delete the brute force notes and stop talking
जवाब देखें

जवाब: Trace it on an example and test edge cases

Testing is part of the score.

पाठ: An Interview Coding Checklist

← कोर्स पर वापस जाएँ