🔥 Real Exam Questions — Batch 2027

TCS NQT Recently Asked Coding Questions

Actual questions from the June 2025 TCS National Qualifier Test with complete solutions in Java, C++, and Python.

📅 5th June 2026 – Q1 & Q2 📅 9th June 2026 – Q3 & Q4

These are verified, student-reported questions from the TCS NQT 2027 (Batch) assessment held in June 2025. Questions 1 & 2 were reported from the 5th June 2026 slot; Questions 3 & 4 from the 9th June 2026 slot. Use this guide to understand the pattern, practice the problems, and go in prepared.

1
Coin Pair Indices (Two Sum Variant)
Easy

Problem Statement

You are given an array of integers representing coin values and an integer target. Find two different indices such that the sum of coin values at those indices equals the target. Return the indices of the two coins.

Example

Input: coins = [2, 7, 11, 15], target = 9 Output: [0, 1] coins[0] + coins[1] = 2 + 7 = 9 ✓

Approach

  • Use a HashMap to store each value and its index as you iterate.
  • For each element, compute complement = target - coins[i].
  • If complement exists in the map, return both indices immediately.
  • Single-pass O(n) — much faster than a brute-force nested loop.
⏱ Time: O(n) 🗂 Space: O(n)

Solution

import java.util.HashMap;

public class Solution {
    public static int[] coinPairIndices(int[] coins, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < coins.length; i++) {
            int complement = target - coins[i];
            if (map.containsKey(complement)) {
                return new int[] { map.get(complement), i };
            }
            map.put(coins[i], i);
        }
        return new int[] { -1, -1 }; // no pair found
    }

    public static void main(String[] args) {
        int[] coins = {2, 7, 11, 15};
        int[] result = coinPairIndices(coins, 9);
        System.out.println("[" + result[0] + ", " + result[1] + "]"); // [0, 1]
    }
}
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

vector<int> coinPairIndices(vector<int>& coins, int target) {
    unordered_map<int, int> mp;
    for (int i = 0; i < coins.size(); i++) {
        int complement = target - coins[i];
        if (mp.count(complement))
            return { mp[complement], i };
        mp[coins[i]] = i;
    }
    return { -1, -1 };
}

int main() {
    vector<int> coins = {2, 7, 11, 15};
    auto res = coinPairIndices(coins, 9);
    cout << "[" << res[0] << ", " << res[1] << "]" << endl; // [0, 1]
}
def coin_pair_indices(coins, target):
    seen = {}
    for i, val in enumerate(coins):
        complement = target - val
        if complement in seen:
            return [seen[complement], i]
        seen[val] = i
    return [-1, -1]

# Test
coins = [2, 7, 11, 15]
print(coin_pair_indices(coins, 9))  # [0, 1]
2
Merge Two Arrays into a Sorted Array
Easy

Problem Statement

You are given two arrays: Array A sorted in ascending order and Array B sorted in descending order. Create a new array containing all elements from both arrays sorted in ascending order.

Example

Input: A = [1, 3, 5, 7] (ascending) B = [10, 8, 6, 4] (descending) Output: [1, 3, 4, 5, 6, 7, 8, 10]

Approach

  • Reverse array B (now it's ascending), or use a reverse pointer.
  • Merge both sorted arrays using the classic two-pointer technique.
  • Compare elements from A and reversed-B, push the smaller one.
  • Flush remaining elements of either array at the end.
⏱ Time: O(n + m) 🗂 Space: O(n + m)

Solution

import java.util.Arrays;

public class MergeArrays {
    public static int[] mergeSorted(int[] A, int[] B) {
        // Reverse B to make it ascending
        int left = 0, right = B.length - 1;
        while (left < right) {
            int tmp = B[left]; B[left] = B[right]; B[right] = tmp;
            left++; right--;
        }
        // Two-pointer merge
        int[] result = new int[A.length + B.length];
        int i = 0, j = 0, k = 0;
        while (i < A.length && j < B.length)
            result[k++] = A[i] <= B[j] ? A[i++] : B[j++];
        while (i < A.length) result[k++] = A[i++];
        while (j < B.length) result[k++] = B[j++];
        return result;
    }

    public static void main(String[] args) {
        int[] A = {1,3,5,7}, B = {10,8,6,4};
        System.out.println(Arrays.toString(mergeSorted(A, B)));
        // [1, 3, 4, 5, 6, 7, 8, 10]
    }
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> mergeSorted(vector<int> A, vector<int> B) {
    reverse(B.begin(), B.end());
    vector<int> result;
    int i = 0, j = 0;
    while (i < A.size() && j < B.size())
        result.push_back(A[i] <= B[j] ? A[i++] : B[j++]);
    while (i < A.size()) result.push_back(A[i++]);
    while (j < B.size()) result.push_back(B[j++]);
    return result;
}

int main() {
    vector<int> A = {1,3,5,7}, B = {10,8,6,4};
    for (auto v : mergeSorted(A, B)) cout << v << " ";
    // 1 3 4 5 6 7 8 10
}
def merge_sorted(A, B):
    B_asc = B[::-1]   # reverse B to ascending
    result = []
    i = j = 0
    while i < len(A) and j < len(B_asc):
        if A[i] <= B_asc[j]:
            result.append(A[i]); i += 1
        else:
            result.append(B_asc[j]); j += 1
    result.extend(A[i:])
    result.extend(B_asc[j:])
    return result

print(merge_sorted([1,3,5,7], [10,8,6,4]))
# [1, 3, 4, 5, 6, 7, 8, 10]
3
First Occurrence of Target in an Array
Easy

Problem Statement

Given an array of integers and a target value, find the index of the first occurrence of the target in the array. If the target is not present, return -1.

Examples

Example 1 — Target found: arr = [4, 2, 7, 2, 9, 2], target = 2 → Output: 1 Target 2 appears at indices 1, 3, 5. First occurrence is index 1.
Example 2 — Target not found: arr = [5, 8, 1, 6], target = 3 → Output: -1

Approach

  • Simple linear scan — iterate left to right.
  • Return the index immediately when arr[i] == target.
  • If the loop ends without a match, return -1.
⏱ Time: O(n) 🗂 Space: O(1)

Solution

public class FirstOccurrence {
    public static int firstOccurrence(int[] arr, int target) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == target) return i;
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {4, 2, 7, 2, 9, 2};
        System.out.println(firstOccurrence(arr, 2)); // 1
        System.out.println(firstOccurrence(arr, 3)); // -1
    }
}
#include <iostream>
#include <vector>
using namespace std;

int firstOccurrence(vector<int>& arr, int target) {
    for (int i = 0; i < arr.size(); i++)
        if (arr[i] == target) return i;
    return -1;
}

int main() {
    vector<int> arr = {4,2,7,2,9,2};
    cout << firstOccurrence(arr, 2) << endl; // 1
    cout << firstOccurrence(arr, 3) << endl; // -1
}
def first_occurrence(arr, target):
    for i, val in enumerate(arr):
        if val == target:
            return i
    return -1

print(first_occurrence([4,2,7,2,9,2], 2))  # 1
print(first_occurrence([5,8,1,6], 3))          # -1
4
Minimum Depth of a Binary Tree
Medium

Problem Statement

Given the root of a binary tree, return its minimum depth — the number of nodes along the shortest path from the root node down to the nearest leaf node. A leaf node has no left or right children.

Example

Input Tree: 1 / \ 2 3 / 4 Output: 2 Shortest path: 1 → 3 (length 2). Node 3 is a leaf.

Approach

  • Use Breadth-First Search (BFS) — guaranteed to reach the nearest leaf first.
  • Process level by level; the moment you encounter a leaf, return the current depth.
  • BFS is preferred over DFS here since DFS explores full branches before finding the minimum.
  • Handle edge cases: null root → return 0; single root (leaf) → return 1.
⏱ Time: O(n) 🗂 Space: O(n) — BFS queue

Solution

import java.util.LinkedList;
import java.util.Queue;

public class MinDepth {
    static class TreeNode {
        int val;
        TreeNode left, right;
        TreeNode(int v) { val = v; }
    }

    public static int minDepth(TreeNode root) {
        if (root == null) return 0;
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        int depth = 1;
        while (!q.isEmpty()) {
            int size = q.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = q.poll();
                if (node.left == null && node.right == null)
                    return depth; // leaf reached!
                if (node.left  != null) q.offer(node.left);
                if (node.right != null) q.offer(node.right);
            }
            depth++;
        }
        return depth;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(4);
        System.out.println(minDepth(root)); // 2
    }
}
#include <iostream>
#include <queue>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left, *right;
    TreeNode(int v) : val(v), left(nullptr), right(nullptr) {}
};

int minDepth(TreeNode* root) {
    if (!root) return 0;
    queue<TreeNode*> q;
    q.push(root);
    int depth = 1;
    while (!q.empty()) {
        int sz = q.size();
        for (int i = 0; i < sz; i++) {
            auto node = q.front(); q.pop();
            if (!node->left && !node->right) return depth;
            if (node->left)  q.push(node->left);
            if (node->right) q.push(node->right);
        }
        depth++;
    }
    return depth;
}

int main() {
    TreeNode* root = new TreeNode(1);
    root->left = new TreeNode(2);
    root->right = new TreeNode(3);
    root->left->left = new TreeNode(4);
    cout << minDepth(root) << endl; // 2
}
from collections import deque

class TreeNode:
    def __init__(self, val=0):
        self.val = val
        self.left = self.right = None

def min_depth(root):
    if not root: return 0
    q = deque([(root, 1)])
    while q:
        node, depth = q.popleft()
        if not node.left and not node.right:
            return depth   # leaf!
        if node.left:  q.append((node.left,  depth + 1))
        if node.right: q.append((node.right, depth + 1))

# Build: 1 → (2→4, 3)
root = TreeNode(1)
root.left, root.right = TreeNode(2), TreeNode(3)
root.left.left = TreeNode(4)
print(min_depth(root))  # 2

💡 TCS NQT Exam Tips & Pattern Analysis

  • Array problems dominate: 3 of 4 questions involved arrays. Master sliding window, two pointers, and HashMap lookups.
  • Tree problems are picking up: Binary tree BFS/DFS is now a regular topic. Practice LeetCode Easy–Medium tree problems.
  • Complexity matters: TCS test cases often include large inputs. Always aim for O(n) or O(n log n) — avoid O(n²) brute force.
  • Edge cases are tested: Target not found, empty arrays, single-node trees — always code defensively.
  • Time limit is tight: Aim to solve Easy questions in under 15 minutes, Medium in 20–25 minutes.
  • Choose your language wisely: Java and Python are safest; C++ is faster but requires careful memory management.
  • Practice on CodeChef / LeetCode: Filter by "TCS NQT" tags or Easy–Medium array and tree questions.