TCS NQT Coding Questions
Coding Section Overview
Topics & Frequency
| Topic | Frequency | Typical Difficulty | Example Problem Types |
|---|---|---|---|
| Arrays | Very High | Easy-Medium | Searching, sorting, subarrays, two-pointer |
| Strings | Very High | Easy-Medium | Pattern matching, anagrams, palindromes |
| Sorting & Searching | High | Medium | Custom sorting, binary search variants |
| Recursion | Medium | Medium | Backtracking, generating subsets/permutations |
| Dynamic Programming | Medium | Medium-Hard | LIS, knapsack variants, grid paths |
| Graphs | Low-Medium | Hard | BFS/DFS traversal, shortest paths, connected components |
| Math & Number Theory | Low | Easy-Medium | GCD, prime checking, modular arithmetic |
Languages Allowed
| Language | Pros | Cons | Best For |
|---|---|---|---|
| C | Fast execution, low overhead | Manual memory management, no STL | Students strong in C fundamentals |
| C++ | STL containers & algorithms, fast execution | Verbose syntax for some operations | Competitive programmers (most popular choice) |
| Java | Rich standard library, Collections framework | Verbose boilerplate, slower I/O | Students with Java coursework background |
| Python | Concise syntax, easy string manipulation | Slower execution, may TLE on large inputs | Quick 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
- •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
| Input Size (n) | Expected Complexity | Approach Hint |
|---|---|---|
| n ≤ 20 | O(2^n) or O(n!) | Brute force, backtracking, bitmask |
| n ≤ 500 | O(n^3) | Triple nested loops, Floyd-Warshall |
| n ≤ 5,000 | O(n^2) | Double nested loops, simple DP |
| n ≤ 10^5 | O(n log n) | Sorting + binary search, merge sort, divide & conquer |
| n ≤ 10^6 | O(n) | Single pass, hash map, two pointers, sliding window |
| n ≤ 10^9 | O(log n) or O(1) | Binary search, math formula, matrix exponentiation |
Sample Coding Problems
Sample Questions
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
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
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 PracticingReady to Test Yourself?
Take a full-length mock test with real exam conditions, timed sections, and AI-powered performance analysis.
Starting at ₹295