TCS NQT 2025
PREVIOUS YEARS
UNIQUE SET OF PROBLEMS
[2018-2024]
TCS NQT 2025
PREVIOUS YEARS UNIQUE SET OF PROBLEMS [2018-2024]
Question 1: Find the Subarray with a Given Sum Imagine you are
analyzing a sequence of financial transactions where each entry is the
gain or loss for the day. You want to find continuous periods where the
sum of the transactions is a specific target amount, which could signify
something important, such as a balance being achieved or a certain
profit target.
Problem Statement: You are given an array of integers, where both
positive and negative values represent daily gains and losses. Your task
is to find all the subarrays within the array that sum up to a given target.
These subarrays can start and end at any position, but they must be
contiguous. You need to list each such subarray.
Example: Input: arr = [3, 4, -7, 1, 3, 3, 1, -4] Target Sum: 0
Output: [3, 4, -7]
[3, 4, -7, 1, 3, 3] [1, 3, 3] [3, 3, 1, -4]
Explanation: Each subarray represents a
series of days where the sum of the gains and losses adds upto the target
sum, which in this case is zero.
Question 2: Find the Longest Subarray with Equal Number of 0s and 1s
Imagine you are observing a sequence of binary responses (0s and 1s)
from a group of people answering a survey. You want to find the
longest continuous sequence where the number of yes (1s) equals the
number of no (0s).
Problem Statement:
Given an array of 0s and 1s, find the longest subarray that contains an
equal number of 0s and 1s.
Example:
Input:
Arr = [1, 0, 0, 1, 0, 1, 1]
Output:
6
Explanation:
The subarray [0, 0, 1, 0, 1, 1] has equal 0s and 1s, and its length is 6.
Question 3: Maximum Profit from Stock Prices You are a trader looking to
make the most profit from buying and selling stocks. You can only
complete one transaction, meaning you need to pick the best day to buy
and the best day to sell.
Problem Statement: Given an array where each element represents the
stock price on a given day, find the maximum profit you can achieve by
buying on one day and selling on another day later.
Example: Input: Prices = [7, 1, 5, 3, 6, 4]
Output: 5
Explanation:
You can buy at a price of 1 and sell at a price of 6, making a profit of 6-1=5.
Question 4: Find Numbers with No Repeated Digits You are tasked
with finding unique product codes that do not have repeated digits
within a certain range. Let’s say you are given two numbers n1 and
n2, representing a range of product codes. Your goal is to find how
many numbers in that range have no repeated digits (e.g., the
number 112 has repeated digits, but 123 does not).
Problem Statement:
Given two integers n1 and n2, where n1 < n2, find how many numbers
within this range have no repeated digits.
Example 1:
Input:
n1 = 11
n2 = 15
Output:
4
Explanation:
The numbers 12, 13, 14, and 15 have no repeated digits, while 11 does.
Example 2:
Input:
n1 = 101
n2 = 200
Output:
72
Explanation:
Out of all numbers between 101 and 200, there are 72 numbers with
no repeated digits.
Question 5: Rearrange Multiples of 10 to the End of the Array Consider you are
managing an inventory of products, and the products that are stored in
multiples of 10 need to be sent to a different warehouse. The goal is to rearrange
the inventory list such that all products which are multiples of 10 are moved to
the end of the list while keeping the relative order of all other products
unchanged.
Problem Statement: Given an array of integers, rearrange the elements so that
all multiples of 10 appear at the end of the array. The order of the non-multiples
of 10 should remain the same.
Example: Input: Arr = [10, 12, 5, 40, 30, 7, 5, 9, 10]
Output: [12, 5, 7, 5, 9, 10, 40, 30, 10]
Explanation: All numbers which are not multiples of 10 stay in their
original
positions, and the multiples of 10 (10, 40, 30, 10) are moved to the end.
Question 6: Format Camel Case String Imagine you are working with a string written
in camel case, where multiple words are joined together without spaces, and each
new word begins with an uppercase letter. Your task is to reformat this string by
placing spaces between words and converting all uppercase letters to lowercase.
Problem Statement: Given a string in camel case format, insert spaces between
the words and convert all characters to lowercase.
Example 1: Input: "ThisIsAnAutomationEra" Output: "this is an automation era"
Example 2: Input: "HeyYou" Output: "hey you" Explanation: For both examples,
spaces are inserted between each word, and all uppercase letters are converted to
lowercase.
Question 7: Find All Anagrams of a String You’re tasked with searching
for hidden patterns in text. In this problem, you need to find all the start
indices of substrings in a given string that are anagrams of a target
word.
Problem Statement: Given a string s and a non-empty string p, find
all the start indices of p's anagrams in s.
Example: Input: S = "cbaebabacd" P = "ABC"
Output: [0, 6]
Explanation: The substring starting at index 0 ("cba") and the one
starting at index 6 ("bac") are anagrams of "abc".
Question 8: Find the Median of Two Sorted Arrays Imagine you are
comparing the incomes of two departments in your company. Each
department has a sorted list of salaries, and you want to find the
median income across both departments combined.
Problem Statement:
Given
sortedtwo sorted arrays arr1 and arr2, find the median of the two
arrays.
Example:
Input:
Arr1 = [1, 3]
Arr2 = [2]
Output:
2
Explanation:
When the arrays are merged, the median of [1, 2, 3] is 2.
Question 9: Cyclically Rotate the Array Clockwise by K Imagine you
have a group of people standing in a circle, and you want to rotate their
positions in such a way that everyone shifts by a few places to the right.
Similarly, given an array of integers, your task is to cyclically rotate the
array clockwise by a given number K, meaning every element moves K
places to the right, and the ones at the end wrap around to the start.
Problem Statement: Given an array of size N and a number K, cyclically
rotate the array K times in the clockwise direction. The order of elements
should remain the same but shifted by K.
Example 1: Input: Arr =
[10, 20, 30, 40, 50] K = 2
Output: [40, 50, 10, 20,
30]
Example 2:
Input: Arr = [10, 20, 30, 40] K = 1
Output: [40, 10, 20, 30]
Explanation: In the first example, after rotating the array by 2 positions,
elements [40, 50] from the end shift to the front, and the others follow in
sequence. The same process applies to the second example with K = 1.
Question 10: Merge Two Sorted Lists Imagine you are helping to
merge two teams of employees, and their names are sorted
alphabetically. Your job is to combine these lists while keeping the
final list in sorted order.
Problem Statement:
Given
using two sorted arrays, merge them into one sorted array without
any extra sorting functions.
Example:
Input:
Arr1 = [1, 3, 5]
Arr2 = [2, 4, 6]
Output:
[1, 2, 3, 4, 5, 6]
Question 11: Count Valid Parentheses You are tasked with checking if a
string of parentheses is balanced. For example, when someone enters
data inside a form, you need to validate that every opening parenthesis (
has a corresponding closing parenthesis ).
Problem Statement:
Given a string containing just the characters ( and ), determine if the input
string is valid. A valid string is one where every open parenthesis has a
corresponding closing parenthesis.
Example:
Input:
Str = "((()))"
Output:
True
Explanation:
The parentheses are balanced, so the output is True.
Question 12: Count Valid Parentheses You are tasked with checking if a
string of parentheses is balanced. For example, when someone enters
data inside a form, you need to validate that every opening parenthesis
( has a corresponding closing parenthesis ).
Problem Statement:
Given a string containing just the characters ( and ), determine if the input
string is valid. A valid string is one where every open parenthesis has a
corresponding closing parenthesis.
Example:
Input:
Str = "((()))"
Output:
True
Explanation:
The parentheses are balanced, so the output is
True.
Question 13: Find the Missing Number in a Sequence Imagine you are
organizing a relay race, and each runner is assigned a number in a
continuous sequence. However, one runner’s number is missing from
the sequence, and you need to find out which one.
Problem Statement:
Given an array of n-1 numbers, where the numbers are between 1 and n,
find the missing number.
Example:
Input:
Arr = [1, 2, 4, 5, 6]
Output:
3
Explanation:
The missing number in the sequence is 3.
Question 14: Find the First Unique Character in a String You’re analyzing
a stream of text messages, and your task is to find the first unique
character that doesn't repeat in the message.
Problem Statement:
Given a string s, find the first non-repeating character in it and return its
index. If it doesn't exist, return -1.
Example:
Input:
Str = "loveleetcode"
Output:
2
Explanation:
The first non-repeating character is 'v', and its index is 2.
Question 15: Reverse Only Vowels in a String Imagine you’re working on
a text-based game where you need to perform special effects on
vowels in words. Your job is to reverse the order of vowels in the string,
while keeping the consonants in place.
Problem Statement:
Given a string, reverse only the vowels of the string.
Example:
Input:
Str = "hello"
Output:
"holle"
Explanation:
The vowels e and o are swapped, while the other letters remain the
same.
NEW SANKALP BATCH 3.0
“सपनेे पूूरे ेकरनेे का व आ गया हैै—चलो मलकर मेेहनत कर! ”
[Last 30 seats Left only]
Complete Placement Prep for Companies Like:
TCS | Capgemini | Wipro | Cognizant | Accenture | and more!
www.campusmonk.in
Question 16: Pairing Employees for a Game You are tasked with organizing
a game for employees in a company. Each employee can either compete
individually or pair up with another employee. If the total number of
employees is odd, no pairs can be formed. If it is even, they can either stay
single or form pairs. Your goal is to calculate how many different ways
employees can either stay single or pair up.
Problem Statement: Given a number N, which represents the total
number of employees, find the number of possible ways they can
compete either as individuals or in pairs. If N is odd, employees must all
remain single.
Example 1:
Input:
N=3
Output:
1
Explanation:
Since the number of employees is odd, there is only one way: all
employees compete as individuals.
Example 2:
Input:
N=4
Output:
10
Explanation:
When N = 4, there are 10 different combinations of employees
competing
individually or in pairs.
Question 17: Find the Sum of Subarray Closest to Zero Imagine you’re analyzing
temperature changes over a series of days, and you want to find the stretch of
days where the change in temperature was closest to zero.
Problem Statement: Given an array of integers representing temperature
changes, find the subarray with a sum closest to zero.
Example: Input: Arr = [1, -2, 3, 4, -5, 6]
Output: 1
Explanation: The subarray [-2, 3] has a sum of 1, which is closest to zero.
Question 18: Find the Subarray with Maximum Sum Imagine you are
evaluating the financial performance of a company over several quarters,
and you want to find the period where the company achieved the highest
profits.
Problem Statement: Given an array of integers representing profits and
losses over a period of time, find the contiguous subarray with the largest
sum.
Example: Input: Arr = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1,2,1] has the largest sum, which is 6.
Question 19: Merge Intervals Imagine you are managing the schedules of
multiple employees, and you want to merge overlapping time slots into a single
time block.
Problem Statement: Given an array of intervals where each interval is
represented as a pair [start, end], merge all overlapping intervals.
Example: Input: Intervals = [[1,3],[2,6],[8,10],[15,18]]
Output:
[[1,6],[8,10],[15,18]]
Explanation: The intervals [1,3] and [2,6] overlap, so they are merged into
[1,6].
Question 20: Convert Roman Numerals to Integer Imagine you’re
working with an ancient system where Roman numerals are used. Your
job is to convert these Roman numerals to integers.
Problem Statement: Given a string representing a Roman numeral,
convert it to an integer.
Example: Input: Str = "IX"
Output: 9
Explanation: IX represents 9.
Question 21: Find the Majority Element Imagine you are counting votes in an
election, and you need to find the candidate who received more than half of the
total votes.
Problem Statement: Given an array of integers, where each integer represents a
vote for a candidate, find the majority element (the element that appears more
than n/2 times).
Example: Input: Arr = [2, 2, 1, 1, 2, 2]
Output: 2
Explanation: 2 is the majority element as it appears 4 times, more than half the
array length.
Question 22: Find the Kth Largest Element You are given a list of prizes
won by participants in a competition. You want to determine the prize that
ranks at a specific position from the top.
Problem Statement: Given an array of integers and an integer K, find the
Kth largest element in the array.
Example: Input: Arr = [3, 2, 1, 5, 6, 4] K = 2
Output:
5
Explanation: The second-largest element is 5.
Question 23: Find the Sum of Subarray Closest to Zero Imagine you’re analyzing
temperature changes over a series of days, and you want to find the stretch of
days where the change in temperature was closest to zero.
Problem Statement: Given an array of integers representing temperature
changes, find the subarray with a sum closest to zero.
Example: Input: Arr = [1, -2, 3, 4, -5, 6]
Output: 1
E xplanation: The subarray [-2, 3] has a sum of 1, which is closest to zero.
Question 24: Find the Subarray with Maximum Sum Imagine you are evaluating
the financial performance of a company over several quarters, and you want to
find the period where the company achieved the highest profits.
Problem Statement: Given an array of integers representing profits and losses over
a period of time, find the contiguous subarray with the largest sum.
Example: Input: Arr = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1,2,1] has the largest sum, which is 6.
Question 25: Merge Intervals Imagine you are managing the schedules of
multiple employees, and you want to merge overlapping time slots into a single
time block.
Problem Statement: Given an array of intervals where each interval is
represented as a pair [start, end], merge all overlapping intervals.
Example: Input: Intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: The intervals [1,3] and [2,6] overlap, so they are merged into [1,6].
Question 26: Find the GCD of Two Numbers Imagine you are tasked with
creating a system to calculate the greatest common divisor (GCD) of two
product codes to ensure compatibility between components.
Problem Statement: Given two integers a and b, return their greatest common
divisor (GCD).
Example: Input: a = 54 b = 24
Output: 6
Explanation: The GCD of 54 and 24 is 6.
Question 27: Find the Subarray with a Given Sum You are analyzing
customer transaction data and want to find a continuous
sequence of transactions that adds up to a specific target amount.
Problem Statement:
Given an array of integers and a target sum, find the subarray that adds up
to
the target sum.
Example:
Input:
Arr = [1,2,3,7,5]
Target = 12
Output:
[2,3,7]
Explanation:
The subarray [2,3,7] adds up to 12.
Question 28: Find the Product of Array Except Self Imagine you are developing
a system to distribute bonuses to employees, where
the bonus is determined by multiplying the performance scores of all other
employees except the one under consideration.
Problem Statement:
Given an array of integers, return an array such that each element at index i is
the product of all elements in the array except the one at index i.
Example:
Input:
Arr = [1,2,3,4]
Output:
[24,12,8,6]
Explanation:
For example, the product of all elements except the element at index 1 is
3*4*1=12.
Question 29: Count the Number of Set Bits Imagine you are working with a
binary communication protocol, and you need to count the number of bits set
to 1 in a transmitted message.
Problem Statement: Given an integer n, return the number of 1 bits it has (also
known as the Hamming weight).
Example: Input: n = 11
Output: 3
Explanation: The binary representation of 11 is 1011, which has three 1 bits.
Question 30: Generate Pascal's Triangle Imagine you are arranging items in a
triangular pattern. Your task is to generate the structure of Pascal’s triangle, a
triangular arrangement where each element is the sum of the two directly
above it.
Problem Statement: Given an integer n, generate the first n rows of Pascal's
triangle.
Example: Input: n = 5
Output: [[1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]
Question 31: Check if a String is a Valid Palindrome Imagine you are
analyzing text for grammatical correctness, and one of the checks involves
verifying if the text reads the same backward as forward, ignoring
punctuation and spaces.
Problem Statement: Given a string, determine if it is a palindrome,
considering only alphanumeric characters and ignoring cases.
Example: Input: Str = "A man, a plan, a canal: Panama"
Output: True
Explanation: The string is a palindrome when ignoring punctuation and
case.