TCS NQT Coding Cheatsheet
TCS NQT Coding Cheatsheet
Arrays
G
sum
IN
Test Case:
Output: 4
D
Rotate a Matrix by 90 Degrees in Clockwise Direction
O
Problem: Given an matrix, rotate the matrix by 90 degrees in the
n x n
Test Case:
Problem: Given an integer array nums , find the subarray with the largest
sum and return its sum.
PR
Test Case:
Output: 6
Test Case:
Test Case:
Output: 3
G
Problem: Given an array of risk levels (integers ranging from 0 to 2),
sort the array based on these risk levels.
IN
Test Case:
Input:
Output: [0, 0, 1, 1, 2, 2] D
riskLevels = [2, 0, 1, 2, 1, 0]
O
Push Empty Packets to the End of the Array
Test Case:
E
Output: [2, 3, 5, 0, 0]
Two Sum
PR
Test Case:
Output: [0, 1]
Source: LeetCode
Test Case:
Output: True
Source: GeeksforGeeks
Test Case:
G
Input: arr = [1, 2, 3, 4]
Output: 10
IN
Source: GeeksforGeeks
Pascal's Triangle
D
Problem: Generate the first few rows of Pascal's Triangle.
Test Case:
O
Input: numRows = 5
C
Output: [[1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]
Source: LeetCode
E
Test Case:
Output: {1: 1, 2: 2, 3: 3}
Source: GeeksforGeeks
Move Zeroes
Problem: Move all zeros in an array to the end while maintaining the
order of non-zero elements.
Test Case:
Source: LeetCode
Test Case:
Output: [1, 2, 3, 4]
Source: GeeksforGeeks
G
Contains Duplicate
IN
Test Case:
Output:
Source: LeetCode
True
D
O
Find Duplicates in O(n) Time and O(1) Extra Space
C
Problem: Find duplicates in an array in linear time and constant space.
Test Case:
E
Output: [2, 3]
Source: GeeksforGeeks
Problem: Rotate an array to the right K times and print the result.
Test Case:
Output: [4, 5, 1, 2, 3]
Source: GeeksforGeeks
Single Number
Problem: Find the element that appears only once in an array where
every other element appears twice.
Test Case:
Output: 4
Source: LeetCode
Test Case:
G
Input: arr = [1, 2, 3, 4, 5, 6, 7]
IN
Source: GeeksforGeeks
D
Problem: Find the smallest and second smallest elements in an array.
Test Case:
O
Input: arr = [12, 13, 1, 10, 34, 1]
C
Output: Smallest: 1, Second Smallest: 10
Source: GeeksforGeeks
E
Test Case:
Output: 1
Source: LeetCode
Test Case:
Output: [8, 8, 8, 2, 2, 5, 5, 6]
Source: GeeksforGeeks
Majority Element
Problem: Find the majority element that appears more than half the time
in an array.
Test Case:
Output: 3
Source: LeetCode
G
Next Greater Element I
IN
Problem: Find the next greater element for each element in an array.
Test Case:
Input:
Output: D
nums1 = [4, 1, 2], nums2 = [1, 3, 4, 2]
[-1, 3, -1]
O
Source: LeetCode
C
Intersection of Two Arrays
Test Case:
Output: [2]
Source: LeetCode
PR
Test Case:
Output: 2
Source: LeetCode
Test Case:
Output: 2
Source: LeetCode
G
subsequence in an array.
Test Case:
IN
Input: arr = [1, 3, 5, 4, 7]
Output:
Source: LeetCode
3
D
O
STRINGS
C
Test Case:
Output:
00006
00028 00066
00120 00190 00276
Test Case:
Output: 3
Test Case:
G
Output: ["apple", "banana", "orange"]
IN
Given String is Palindrome or Not
D
Problem: Check if a given string is a palindrome.
Test Case:
O
Input: str = "madam"
C
Output: True
Source: GeeksforGeeks
E
Test Case:
Source: LeetCode
Remove Character
Problem: Remove characters from the first string that are present in the
second string.
Test Case:
Output: "bde"
Source: GeeksforGeeks
Reverse a String
Test Case:
Output: "olleh"
Source: GeeksforGeeks
G
Remove All Adjacent Duplicates in String
IN
Test Case:
Output:
Source: LeetCode
"ca"
D
O
Uncommon Words from Two Sentences
C
Problem: Find the uncommon words from two sentences.
Test Case:
E
Source: LeetCode
Test Case:
Output: 28
Source: LeetCode
Test Case:
Output: g2 e4 k2 s2 f1 o1 r1
Source: GeeksforGeeks
Test Case:
G
Input: str = "cba"
Output: "abc"
IN
Source: GeeksforGeeks
Valid Anagram
D
Problem: Check if two strings are anagrams.
Test Case:
O
Input: str1 = "anagram", str2 = "nagaram"
C
Output: True
Source: LeetCode
E
Test Case:
Output: "hELLO"
Source: GeeksforGeeks
Test Case:
Input: s = "00110011"
Output: 6
Source: LeetCode
Test Case:
Output: 3
Source: GeeksforGeeks
G
Count Unique Characters of All Substrings of a Given String
IN
substrings of a given string.
Test Case:
Input:
Output:
s = "ABC"
10 D
O
Source: LeetCode
C
Find the Difference
Problem: Find the difference between two strings where one string is
the original string with an extra character.
E
Test Case:
IM
Output: "e"
PR
Source: LeetCode
Test Case:
Output: t2 s2
Source: GeeksforGeeks
Test Case:
Source: GeeksforGeeks
Detect Capital
G
Test Case:
IN
Input: word = "USA"
Output: True
Source: LeetCode
Fizz Buzz D
O
Problem: Print the numbers from 1 to n. But for multiples of three print
“Fizz” instead of the number and for the multiples of five print “Buzz”.
C
Test Case:
Input:
E
n = 15
Output: ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz",
IM
Source: LeetCode
Test Case:
Output: True
Source: GeeksforGeeks
SORTING
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
Mathematical
G
Sum of the Cubes of All Integers Within a Given Range [n, m]
IN
Problem: Given two integers n and m , calculate the sum of the cubes
of all integers within the range [n, m] .
Test Case:
D
O
Input: n = 1, m = 3
Problem: Given two integers n and k , find the k-th largest factor of n .
E
Test Case:
IM
Input: n = 12, k = 2
Output: 4
PR
Test Case:
Input: n = 10
Output: 5
Test Case:
Input: n = 153
Output: True
G
Test Case:
Input:
IN
N = 234
Output: 24 (2 * 3 * 4)
Test Case:
C
Input: N = 9875, R = 2
Output: 2
E
Test Case:
PR
Input: n = 6, p = 2
Output: 1
Climbing Stairs
Problem: Given n steps, find the number of distinct ways to reach the
top.
Test Case:
Input: n = 3
Output: 3
Source: LeetCode
Test Case:
Output: True
Source: GeeksforGeeks
G
Prime Numbers
IN
Test Case:
Input: n = 10
Output: [2, 3, 5, 7]
Test Case:
Input: num = -3
IM
Source: GeeksforGeeks
PR
Test Case:
Input: n = 12
Source: GeeksforGeeks
Test Case:
Input: num = 26
Output: "1a"
Source: LeetCode
Test Case:
G
Input: num = 16
Output: True
IN
Source: LeetCode
D
Problem: Given two fractions, find their sum and return it in the simplest
form.
O
Test Case:
C
Input: frac1 = 1/2, frac2 = 1/3
Output: 5/6
E
Source: GeeksforGeeks
Fibonacci Numbers
IM
Test Case:
PR
Input: n = 5
Output: 5
Source: GeeksforGeeks
Add Digits
Problem: Given an integer num , repeatedly add its digits until the result
is a single digit.
Test Case:
Input: num = 38
Output: 2
Source: LeetCode
Test Case:
Output: 1525
G
Source: GeeksforGeeks
Perfect Number
IN
Problem: Determine whether a given number is a perfect number.
Test Case:
Input:
Output:
num = 28
D
O
True
Source: GeeksforGeeks
C
Armstrong Numbers
Test Case:
IM
Output: True
Source: GeeksforGeeks
PR
Test Case:
Input: n = 5
Output: 15
Source: GeeksforGeeks
Test Case:
Input: N = 4
Output: 6
Source: GeeksforGeeks
G
its roots.
Test Case:
IN
Input: a = 1, b = -3, c = 2
Output: Roots = 2, 1
Source: GeeksforGeeks D
O
Maximum Product of Three Numbers
Test Case:
E
Output: 500
Source: LeetCode
Happy Number
PR
Test Case:
Input: n = 19
Output: True
Source: LeetCode
ADVANCED
Test Case:
Input: N = 5
Output: 48
G
Problem: Given an array of risk levels (integers ranging from 0 to 2),
sort the array based on these risk levels.
IN
Test Case:
Output: [0, 0, 1, 1, 2, 2]
D
O
International Conference Round Table Seating
Test Case:
E
Input: N = 6
IM
Output: 240
Test Case:
Output: $15
Test Case:
Input: n = 30
Output: 4
Test Case:
Output: True
G
Source: Detect Loop in a Linked List
IN
Problem: Print the middle element of a linked list.
Test Case:
Output:
D
1 -> 2 -> 3 -> 4 -> 5
O
3
Source: GeeksforGeeks
C
Can We Reverse a Linked List in Less than O(n)?
Problem: Discuss if it's possible to reverse a linked list in less than O(n)
E
time.
Test Case:
IM
Output: No
PR
Source: GeeksforGeeks
Linear Search
Problem: Search for a target element in a given array using the linear
search method.
Test Case:
Output: 3
Source: GeeksforGeeks
Binary Search
Problem: Search for a target element in a sorted array using the binary
search method.
Test Case:
Output: 3
G
IN
D
O
C
E
IM
PR