CS Fundamentals · Fresher-relevant
Algorithm interview questions on sorting, searching, dynamic programming, greedy approaches, and Big-O complexity analysis.
What is Big-O notation and why does it matter?
Tip: Big-O describes how runtime/space grows with input size in the worst case, ignoring constants. It lets you compare algorithms independent of hardware: O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ). Always state both time and space complexity.
Explain the difference between merge sort and quick sort.
Tip: Merge sort is O(n log n) guaranteed, stable, but needs O(n) extra space. Quick sort is O(n log n) average / O(n²) worst (mitigated by random/median pivots), in-place, and usually faster in practice due to cache locality. Pick merge sort for stability/linked lists, quick sort for in-memory arrays.
What is dynamic programming and when do you apply it?
Tip: DP solves problems with optimal substructure and overlapping subproblems by caching subresults — top-down (memoisation) or bottom-up (tabulation). Classic examples: knapsack, longest common subsequence, edit distance, coin change. Define the state and recurrence first, then optimise space.
What is the difference between greedy and dynamic programming approaches?
Tip: Greedy makes the locally optimal choice at each step and never reconsiders — fast and correct only when the problem has the greedy-choice property (e.g. activity selection, Dijkstra). DP explores combinations of choices, so it is correct when greedy fails (e.g. 0/1 knapsack).
Explain binary search and its prerequisite.
Tip: Binary search finds a target in a sorted array in O(log n) by repeatedly halving the search space. The prerequisite is sorted (or monotonic) data. Watch for off-by-one bugs: use low <= high and mid = low + (high-low)/2 to avoid overflow.
What is the two-pointer technique and a sliding window?
Tip: Two pointers move through data from both ends or at different speeds to avoid nested loops (pair-sum in a sorted array). A sliding window maintains a contiguous range, expanding/shrinking to satisfy a constraint — turning many O(n²) substring/subarray problems into O(n).
How does Dijkstra's shortest-path algorithm work?
Tip: Dijkstra greedily expands the closest unvisited node using a min-heap, relaxing edges to update tentative distances — O((V+E) log V). It requires non-negative edge weights; for negative weights use Bellman-Ford. It computes single-source shortest paths.
What is recursion and how does it relate to the call stack?
Tip: Recursion solves a problem via smaller instances of itself, needing a base case to terminate. Each call adds a stack frame, so deep recursion risks stack overflow — convert to iteration or use tail-call/explicit stack. Recursion shines for trees, divide-and-conquer, and backtracking.
InterviewEra generates role-specific questions using your actual projects and skills. Get scored feedback on technical depth, clarity, and structure — free to start.