Top 50 Coding Interview Questions Every Fresher Should Practice

By Preeti Verma

Placement and fresher coding rounds draw from a small pool of problems. You do not need to solve 500 questions. You need to solve the right 50 well, and understand the pattern behind each one.

This list is grouped by topic and ordered from easy to harder inside each group. For every problem you get the core idea and the complexity to quote. Try the problem yourself first for 15 to 20 minutes, then read the idea. If you cannot solve it, that is useful information, not failure: write down the pattern and come back in two days.

How to practise these 50 questions

  1. Solve in the language you will use in the interview.
  2. Say the brute-force approach first, then improve it. Interviewers want to hear that progression.
  3. State time and space complexity every time.
  4. Test with an empty input, a single element and a duplicate-heavy input before you say "done".
  5. Repeat each problem a week later without looking.

Arrays (10)

  1. Find the largest and second largest element. One pass, track two variables. O(n) time, O(1) space.
  2. Reverse an array in place. Two pointers swapping from both ends. O(n), O(1).
  3. Move all zeros to the end. Keep a write index for non-zero values, then fill zeros. O(n), O(1).
  4. Find the missing number from 1 to n. Expected sum minus actual sum, or XOR. O(n), O(1).
  5. Find duplicates in an array. Use a set, or mark visited indexes if values are in range. O(n).
  6. Two Sum. Store each value's index in a hash map and look up target - value. O(n), O(n).
  7. Maximum subarray sum (Kadane). Keep a running sum, reset it to zero when it goes negative, track the best. O(n), O(1).
  8. Rotate an array by k positions. Reverse the whole array, then reverse the first k and the rest. O(n), O(1).
  9. Merge two sorted arrays. Two pointers, always take the smaller front element. O(n + m).
  10. Best time to buy and sell a stock. Track the minimum price so far and the best profit. O(n), O(1).

A quick example of Kadane, because it is asked constantly:

public static int maxSubarray(int[] nums) {
    int best = nums[0], current = nums[0];
    for (int i = 1; i < nums.length; i++) {
        current = Math.max(nums[i], current + nums[i]);
        best = Math.max(best, current);
    }
    return best;
}

Strings (10)

  1. Reverse a string. Two pointers or a builder. O(n).
  2. Check if a string is a palindrome. Two pointers from both ends, skipping non-alphanumeric characters. O(n).
  3. Check if two strings are anagrams. Count characters in an array of 26, or sort both. O(n).
  4. Count vowels and consonants. Single pass with a set of vowels. O(n).
  5. Find the first non-repeating character. Count with a map, then scan again in order. O(n).
  6. Remove duplicates from a string. Keep a set of seen characters and a builder. O(n).
  7. Longest common prefix of an array of strings. Compare column by column until a mismatch. O(total characters).
  8. Reverse the words in a sentence. Split on spaces, reverse the list, join. Mention handling extra spaces.
  9. Valid parentheses. Push openers on a stack, pop and match on closers. The stack must be empty at the end. O(n).
  10. Longest substring without repeating characters. Sliding window with a set or a map of last positions. O(n).

Hash maps and sets (8)

  1. Count the frequency of each element. One pass into a map. O(n).
  2. Find the first repeated element. Walk with a set. O(n).
  3. Group anagrams. Use the sorted string (or a count signature) as the map key. O(n * k log k).
  4. Find the intersection of two arrays. Put one in a set, scan the other. O(n + m).
  5. Subarray with a given sum (positive numbers). Sliding window. For arrays with negatives, use a prefix-sum map.
  6. Longest consecutive sequence. Put all numbers in a set, start counting only from numbers with no predecessor. O(n).
  7. Check if an array contains duplicates within k distance. Map from value to last index. O(n).
  8. Top k frequent elements. Count with a map, then use a heap or bucket sort. O(n log k).

Sorting and searching (8)

  1. Binary search. Halve the range each step; watch the loop condition and mid calculation. O(log n).
  2. Find the first and last position of an element. Binary search twice, biased left and right. O(log n).
  3. Search in a rotated sorted array. Binary search, decide which half is sorted at each step. O(log n).
  4. Find the square root of a number (integer). Binary search on the answer. O(log n).
  5. Bubble, selection and insertion sort. Know how each works and why all are O(n squared). Freshers get asked to write at least one.
  6. Merge sort. Divide, sort halves, merge. O(n log n) time, O(n) space.
  7. Quick sort. Choose a pivot, partition, recurse. Average O(n log n), worst case O(n squared).
  8. Sort an array of 0s, 1s and 2s. Dutch national flag with three pointers. O(n), one pass.

Binary search is where small mistakes hide, so learn one template and stick to it:

def binary_search(nums, target):
    lo, hi = 0, len(nums) - 1
    while lo <= hi:
        mid = lo + (hi - lo) // 2
        if nums[mid] == target:
            return mid
        if nums[mid] < target:
            lo = mid + 1
        else:
            hi = mid - 1
    return -1

Linked lists, stacks and queues (8)

  1. Reverse a linked list. Iterate with three pointers: previous, current, next. O(n), O(1).
  2. Find the middle of a linked list. Slow and fast pointers. O(n).
  3. Detect a cycle in a linked list. Floyd's slow and fast pointers meet if there is a cycle. O(n).
  4. Merge two sorted linked lists. Walk both, link the smaller node each time. O(n + m).
  5. Remove the nth node from the end. Move a fast pointer n steps ahead, then move both. One pass.
  6. Implement a stack using an array or list. Know push, pop, peek and the empty check.
  7. Implement a queue using two stacks. Push to one stack, move to the second stack when you need to pop.
  8. Next greater element. Monotonic stack, scanning from the right. O(n).

Recursion and basic dynamic programming (6)

  1. Factorial and Fibonacci. Write recursive first, then show the memoised or iterative version. Explain why naive Fibonacci is exponential.
  2. Power of a number (fast exponentiation). Halve the exponent each step. O(log n).
  3. Generate all subsets of an array. Recursive include or exclude choice. O(2^n).
  4. Climbing stairs. ways(n) = ways(n-1) + ways(n-2). The simplest dynamic programming problem.
  5. Coin change (minimum coins). DP over amounts. A good first "real" DP question.
  6. Longest common subsequence. A 2D DP table over both strings. Know the table, not just the code.

If dynamic programming feels scary, start with our dynamic programming guide for beginners.

The patterns behind the 50

Notice how few ideas repeat:

  • Two pointers: reversing, palindromes, merging, sorted-array problems.
  • Hash map or set: counting, duplicates, Two Sum, anagrams.
  • Sliding window: longest substring, subarray sums.
  • Binary search: anything sorted, or any answer you can test as "too small or big enough".
  • Stack: parentheses, next greater element.
  • Recursion with choices: subsets, permutations, Fibonacci.

Learning the pattern is how 50 problems prepare you for 500. For the next step up, our DSA patterns guide maps the 12 patterns to the Blind 75 and NeetCode 150 lists.

How to explain your solution in the interview

The code is half of the score. Use this order:

  1. Repeat the problem in your own words and ask about edge cases.
  2. Give the brute-force idea and its complexity.
  3. Improve it, and say why the new idea is faster.
  4. Write the code while narrating what each part does.
  5. Walk through one example by hand.
  6. State final time and space complexity.

This is exactly what a spoken mock interview trains. The GenQuest AI mock interview lets you practise explaining a solution out loud and answering follow-ups, and the question banks help you find the topics you keep missing.

A worked example: solving Two Sum step by step

Two Sum is on almost every list, so it is worth seeing how a good answer unfolds. The problem: given an array of integers and a target, return the positions of the two numbers that add up to the target.

Step 1, say the brute force. Check every pair with two loops. That is correct, but it takes O(n squared) time. Saying this first shows you understand the problem before you optimise.

Step 2, find the waste. For each number x, you are really asking "have I already seen target - x?" Searching the array again for that value is the slow part.

Step 3, use a hash map. Store each value with its index as you walk the array. For each number, check whether its complement is already in the map.

def two_sum(nums, target):
    seen = {}                       # value -> index
    for i, x in enumerate(nums):
        need = target - x
        if need in seen:
            return [seen[need], i]
        seen[x] = i
    return []

Step 4, state the complexity. One pass, so O(n) time, and the map uses O(n) extra space. You traded memory for speed.

Step 5, test it. Try [2, 7, 11, 15] with target 9 (expect positions 0 and 1). Then an edge case: [3, 3] with target 6, where the same value appears twice. Storing the value after the check, as above, handles it correctly. Finally, say what you return if no pair exists.

That five-step shape works for every problem on this list: brute force, find the waste, improve, state the complexity, test.

Edge cases to check on every problem

Most lost marks in coding rounds come from edge cases, not from the main idea. Before you say "done", run through this list:

  • An empty input, or a single element.
  • All elements equal, or many duplicates.
  • Negative numbers and zero.
  • Already sorted or reverse-sorted input.
  • Very large values that could overflow, in languages with fixed-size integers.
  • Strings with spaces, punctuation or mixed case.
  • A target that does not exist in the input.

Say them aloud as you test. Interviewers notice candidates who test their own code, and it often catches the bug before they do.

Frequently asked questions

How many coding questions should a fresher solve?

Quality matters more than count. Fifty well-understood problems across the topics above are better than 300 half-remembered ones. Re-solve the ones you missed after a week.

Which language is best for coding rounds?

The one you are most fluent in. Java, Python and C++ are all accepted at most companies. Pick one and learn its standard library: lists, maps, sets, sorting and string handling.

How do I improve speed?

Solve with a timer, 20 to 30 minutes per easy or medium problem. Speed comes from recognising patterns, not typing faster.

Do service companies ask harder questions than this?

Usually the opposite: many service-company online tests stay at easy and medium level. Product companies go deeper into dynamic programming, graphs and design. Start with this list either way.

The takeaway

Fifty problems, grouped by pattern, practised properly, cover most fresher coding rounds. Solve each one yourself, say the complexity, test the edge cases, and revisit what you missed. Consistency over a few weeks beats a last-minute rush.