New Leaf

TCS NQT Coding Questions

Coding Section Overview

The coding portion of the TCS NQT Advanced section presents 2-3 programming problems to be solved in 45-60 minutes. You can write your solution in C, C++, Java, or Python. The problems are evaluated against multiple hidden test cases including edge cases, and partial marks are typically awarded. This section is the primary differentiator between Digital and Prime track selections.

Topics & Frequency

The coding problems in TCS NQT draw from a focused set of DSA topics. Here's what to expect based on historical patterns.
TopicFrequencyTypical DifficultyExample Problem Types
ArraysVery HighEasy-MediumSearching, sorting, subarrays, two-pointer
StringsVery HighEasy-MediumPattern matching, anagrams, palindromes
Sorting & SearchingHighMediumCustom sorting, binary search variants
RecursionMediumMediumBacktracking, generating subsets/permutations
Dynamic ProgrammingMediumMedium-HardLIS, knapsack variants, grid paths
GraphsLow-MediumHardBFS/DFS traversal, shortest paths, connected components
Math & Number TheoryLowEasy-MediumGCD, prime checking, modular arithmetic

Languages Allowed

You can typically choose from four programming languages. Pick the one you're most comfortable with — speed and correctness matter more than language choice.
LanguageProsConsBest For
CFast execution, low overheadManual memory management, no STLStudents strong in C fundamentals
C++STL containers & algorithms, fast executionVerbose syntax for some operationsCompetitive programmers (most popular choice)
JavaRich standard library, Collections frameworkVerbose boilerplate, slower I/OStudents with Java coursework background
PythonConcise syntax, easy string manipulationSlower execution, may TLE on large inputsQuick prototyping, string-heavy problems

If you choose Python, be mindful of time limits. For problems with large input sizes (10^5+), Python may hit Time Limit Exceeded even with correct logic. Consider using sys.stdin for faster I/O and avoid unnecessary list comprehensions in tight loops.

Problem-Solving Framework

Follow this systematic approach for every coding problem. Rushing to code without thinking through the approach is the most common mistake.
  • Step 1 — Read Carefully: Understand the problem statement, constraints, and examples. Note the input size — it hints at the expected time complexity.
  • Step 2 — Identify the Pattern: Is it a sliding window? Two pointers? Sorting + binary search? DP? Categorizing the problem is half the battle.
  • Step 3 — Plan Your Approach: Write pseudo-code or outline the algorithm before coding. Consider edge cases: empty input, single element, all duplicates, negative numbers.
  • Step 4 — Code It: Write clean, readable code. Use meaningful variable names. Handle input/output format exactly as specified.
  • Step 5 — Test Mentally: Trace through the provided examples by hand. Then think of edge cases: minimum input, maximum input, boundary conditions.
  • Step 6 — Optimize If Needed: If your solution is O(n^2) and n can be 10^5, you likely need an O(n log n) or O(n) approach.

Time Complexity Guide

Use input constraints to determine the expected time complexity of your solution. If your approach exceeds these limits, optimize before submitting.
Input Size (n)Expected ComplexityApproach Hint
n ≤ 20O(2^n) or O(n!)Brute force, backtracking, bitmask
n ≤ 500O(n^3)Triple nested loops, Floyd-Warshall
n ≤ 5,000O(n^2)Double nested loops, simple DP
n ≤ 10^5O(n log n)Sorting + binary search, merge sort, divide & conquer
n ≤ 10^6O(n)Single pass, hash map, two pointers, sliding window
n ≤ 10^9O(log n) or O(1)Binary search, math formula, matrix exponentiation

Sample Coding Problems

These problems represent the difficulty range you'll encounter in the TCS NQT coding section. We provide the problem statement and approach — practice implementing the full solution yourself.

Sample Questions

Question 1
easyArrays

Find the Second Largest Element in an Array Given an array of n integers, find the second largest element. If no second largest element exists (all elements are the same), return -1. Constraints: - 2 ≤ n ≤ 10^5 - -10^9 ≤ arr[i] ≤ 10^9 Example: Input: [12, 35, 1, 10, 34, 1] Output: 34 Input: [5, 5, 5, 5] Output: -1

Question 2
mediumStrings

Check if Two Strings are Anagrams Given two strings s1 and s2, determine if they are anagrams of each other. Two strings are anagrams if they contain the same characters with the same frequencies, ignoring case. Constraints: - 1 ≤ |s1|, |s2| ≤ 10^5 - Strings contain only English letters (a-z, A-Z) Example: Input: s1 = "Listen", s2 = "Silent" Output: true Input: s1 = "Hello", s2 = "World" Output: false

Question 3
hardDynamic Programming

Length of the Longest Increasing Subsequence (LIS) Given an array of n integers, find the length of the longest strictly increasing subsequence. A subsequence is a sequence derived from the array by deleting some or no elements without changing the order of the remaining elements. Constraints: - 1 ≤ n ≤ 10^4 - -10^4 ≤ arr[i] ≤ 10^4 Example: Input: [10, 9, 2, 5, 3, 7, 101, 18] Output: 4 Explanation: The longest increasing subsequence is [2, 3, 7, 101] or [2, 5, 7, 101], both of length 4. Input: [0, 1, 0, 3, 2, 3] Output: 4 Explanation: The longest increasing subsequence is [0, 1, 2, 3].

Want more practice questions?

Sign up for full access to hundreds of practice questions with detailed solutions.

Start Practicing

Ready to Test Yourself?

Take a full-length mock test with real exam conditions, timed sections, and AI-powered performance analysis.

Starting at ₹295

Start Mock Test