Professional Documents
Culture Documents
TCS NQT 2023 Coding Questions With Codes Hiringhus
TCS NQT 2023 Coding Questions With Codes Hiringhus
TCS NQT 2023 Coding Questions With Codes Hiringhus
Problem Statement
Aayush is working on a strange algorithm where he wants to convert a string
from A to B. Both the strings are of equal length N.
Below are the rules which can be performed to convert the string:
Let's be the letter which alphabetically comes before all other letters in this
subset. Let 's' be called the smallest elements of subset with this letter 's'
Replace the smallest with all the other elements in subset, then place all the
respective elements back to their respective index.
Function Description
def min_moves_to_convert(N: int, A: str, B: str) -> int:
pass
Input
N: an integer representing the length of the strings (1 <= N <= 1000)
A: a string of length N
B: a string of length N
Output
Returns an integer, representing the minimum number of moves required to
convert string A to string B. If it's not possible, return -1.
Constraints
Only lower case letters of the English alphabet are used in the strings
Test Cases
Test Case 1
Input
N = 2
A = "de"
B = "cd"
Output
-1
Input
N = 5
A = "abcab"
B = "aabab"
Output
Python Code
N = int(input())
A = input()
B = input()
i = 0
res, res2 = "", ""
for x in range(len(A)):
if A[x] != B[x]:
res += A[x]
res2 += B[x]
for x in set(res2):
if x not in A:
print(-1)
break
else:
print(len(set(res2)))
Given a 2D binary matrix filled with 0s and 1s, find the largest
square sub-matrix containing only 1s and return its side length.
Formally, given an m×n binary matrix matrix, find the side length of the largest
square sub-matrix with all 1s.
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
The largest square sub-matrix with all 1s in this matrix has a side length of 2.
The problem is often solved using dynamic programming, where the goal is to
efficiently compute the size of the largest square sub-matrix with all 1s.
Test Case 1:
Input:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Expected Output:
1 1
1 1
Its side length is 2, and the top-left corner is at coordinates (1, 2) and the
bottom-right corner is at coordinates (2, 3).
Test Case 2:
Input:
1 1 1 0
1 1 1 1
0 1 1 0
Expected Output:
Explanation:
In the given input matrix, the largest square sub-matrix with all 1s is:
1 1
1 1
Its side length is 2, and the top-left corner is at coordinates (0, 0) and the
bottom-right corner is at coordinates (1, 1).
rows = len(matrix)
cols = len(matrix[0])
# Example usage:
matrix = [
[1, 0, 1, 0, 0],
[1, 0, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 0, 0, 1, 0]
]
Example usage:
matrix = [
[1, 0, 1, 0, 0],
[1, 0, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 0, 0, 1, 0]
]
size, coordinates = max_square_submatrix(matrix)
print("Maximum size of square sub-matrix with all 1s:", size)
print("Coordinates of top-left and bottom-right corners of the square sub-
matrix:", coordinates)
Problem Description:
A pair (a,b) is symmetric to another pair (c,d) if and only if a=d and b=c
Example:
pairs = [(1, 2), (3, 4), (2, 1), (4, 3), (5, 6)]
(1, 2)
(3, 4)
(2, 1)
(4, 3)
(5, 6)
Symmetric pairs: [(1, 2), (2, 1), (3, 4), (4, 3)]
Explanation:
Test Case 1:
Input:
pairs = [(1, 2), (3, 4), (2, 1), (4, 3), (5, 6)]
Expected Output:
Symmetric pairs: [(1, 2), (2, 1), (3, 4), (4, 3)]
Test Case 2:
Input:
pairs = [(1, 2), (2, 3), (4, 5), (5, 4), (6, 7)]
Expected Output:
In Test Case 1, the symmetric pairs are (1, 2) and (2, 1), and (3, 4) and (4, 3). In
Test Case 2, only the pair (4, 5) and (5, 4) are symmetric to each other.
def find_symmetric_pairs(pairs):
symmetric_pairs = []
pair_dict = {}
return symmetric_pairs
# Test Case 1
pairs1 = [(1, 2), (3, 4), (2, 1), (4, 3), (5, 6)]
symmetric_pairs1 = find_symmetric_pairs(pairs1)
print("Symmetric pairs for Test Case 1:", symmetric_pairs1)
# Test Case 2
pairs2 = [(1, 2), (2, 3), (4, 5), (5, 4), (6, 7)]
symmetric_pairs2 = find_symmetric_pairs(pairs2)
print("Symmetric pairs for Test Case 2:", symmetric_pairs2)
Test Case 1
pairs1 = [(1, 2), (3, 4), (2, 1), (4, 3), (5, 6)]
symmetric_pairs1 = find_symmetric_pairs(pairs1)
print("Symmetric pairs for Test Case 1:", symmetric_pairs1)
Test Case 2
pairs2 = [(1, 2), (2, 3), (4, 5), (5, 4), (6, 7)]
symmetric_pairs2 = find_symmetric_pairs(pairs2)
print("Symmetric pairs for Test Case 2:", symmetric_pairs2)
Problem Statement:
Function Description:
Parameters:
arr (List[int]): The input array to rotate.
d (int): The number of positions to rotate the arra
y by.
Returns:
List[int]: The rotated array.
"""
pass
Constraints:
T hearraycontainsintegers.
0 ≤ len(arr)0 ≤ d ≤ len(arr)
Test Case 1:
Input:
arr = [1, 2, 3, 4, 5, 6, 7]
d = 2
Expected Output:
Test Case 2:
Input:
Expected Output:
Code:
# Test Case 1
arr1 = [1, 2, 3, 4, 5, 6, 7]
d1 = 2
rotated_arr1 = rotate_array(arr1, d1)
print("Rotated array for Test Case 1:", rotated_arr1)
# Test Case 2
arr2 = [10, 20, 30, 40, 50, 60]
d2 = 3
rotated_arr2 = rotate_array(arr2, d2)
print("Rotated array for Test Case 2:", rotated_arr2)
Problem Statement
Given a string containing various characters, including alphabets, digits, and
special characters, our task is to remove all characters from the string except
alphabets (A-Z and a-z) and return the resulting string.
Approach
We can achieve this by iterating over each character in the input string and
selecting only the alphabetic characters using the isalpha() method available in
Python.
pythonCopy code
def remove_non_alphabets(input_string: str) -> str:
Parameters:
input_string (str): The input string containing var
ious characters.
Returns:
str: The resulting string with only alphabets.
"""
return ''.join(char for char in input_string if char.is
alpha())
Test Case
Test Case 1:
Expected Output:
"HelloWorld"
Test Case 2:
"Thequickbrownfoxjumpsoverthelazydog"
Code in Python3
Parameters:
input_string (str): The input string containing var
ious characters.
Returns:
str: The resulting string with only alphabets.
"""
return ''.join(char for char in input_string if char.is
alpha())
# Test Case 1
input_string1 = "Hello123, World!"
output1 = remove_non_alphabets(input_string1)
print("Output for Test Case 1:", output1)
# Test Case 2
input_string2 = "The quick brown fox jumps over the lazy do
g."
output2 = remove_non_alphabets(input_string2)
print("Output for Test Case 2:", output2)
Test Case 1
Test Case 2
input_string2 = "The quick brown fox jumps over the lazy dog."
output2 = remove_non_alphabets(input_string2)
print("Output for Test Case 2:", output2)
Problem Statement
Given a positive integer 'N' and another positive integer 'K', your task is to find
out the 'K'th largest factor of 'N'. If 'K' is more than the number of factors of 'N',
return -1.
Function Description
You need to implement the following function:
Parameters:
n (int): A positive integer.
k (int): A positive integer representing the positi
on of the factor.
Returns:
int: The kth largest factor of n if it exists, othe
Constraints
1 ≤ n ≤ 10^5
1 ≤ k ≤ 10^5
Test Case 1
Input:
n = 10
k = 2
Expected Output:
Explanation:
The factors of 10 are 1, 2, 5, 10. The 2nd largest factor is 5.
Test Case 2
Input:
n = 12
k = 4
Expected Output:
Explanation:
The factors of 12 are 1, 2, 3, 4, 6, 12. The 4th largest factor is 3.
Code in Python3
# Test Case 1
n1 = 10
k1 = 2
output1 = kth_largest_factor(n1, k1)
print("Output for Test Case 1:", output1)
# Test Case 2
n2 = 12
k2 = 4
output2 = kth_largest_factor(n2, k2)
print("Output for Test Case 2:", output2)