TCS NQT 2023 Coding Questions With Codes Hiringhus

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

TCS NQT 2023 coding

Questions with codes


hiringhustle

asked 2022 Aug for 2023 Batch

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:

String A & B are of equal length N

Both of them are in lower case letters

Choose a subset X from the string A between the index 1 and N

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.

TCS NQT 2023 coding Questions with codes hiringhustle 1


Your task is to find the minimum number of moves which is required to perform
this conversion. if it is not possible to convert the string from A to B, then return
-1.

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

TCS NQT 2023 coding Questions with codes hiringhustle 2


Test Case 2

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)))

Maximum size square sub-matrix with all


1s

TCS NQT 2023 coding Questions with codes hiringhustle 3


-asked for 2022 Aug for 2023 Batch

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.

For example, consider the following input matrix:

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:

Side length of the largest square sub-matrix with all 1s: 2

Coordinates of top-left and bottom-right corners of the square sub-matrix:


[(1, 2), (2, 3)]

TCS NQT 2023 coding Questions with codes hiringhustle 4


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 (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:

Side length of the largest square sub-matrix with all 1s: 2

Coordinates of top-left and bottom-right corners of the square sub-matrix:


[(0, 0), (1, 1)]

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).

TCS NQT 2023 coding Questions with codes hiringhustle 5


def max_square_submatrix(matrix):
if not matrix:
return 0, []

rows = len(matrix)
cols = len(matrix[0])

# Initialize dp[][] with the same values as the first r


ow and first column of the input matrix
dp = [[0] * cols for _ in range(rows)]
max_size = 0
max_row, max_col = 0, 0

# Copy the first row


for j in range(cols):
dp[0][j] = matrix[0][j]
if dp[0][j] == 1:
max_size = 1
max_row, max_col = 0, j

# Copy the first column


for i in range(rows):
dp[i][0] = matrix[i][0]
if dp[i][0] == 1:
max_size = 1
max_row, max_col = i, 0

# Fill dp[][] table


for i in range(1, rows):
for j in range(1, cols):
if matrix[i][j] == 1:
dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i
-1][j-1]) + 1
if dp[i][j] > max_size:
max_size = dp[i][j]
max_row, max_col = i, j

TCS NQT 2023 coding Questions with codes hiringhustle 6


return max_size, [(max_row - max_size + 1, max_col - ma
x_size + 1), (max_row, max_col)]

# 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:", siz
e)
print("Coordinates of top-left and bottom-right corners of
the square sub-matrix:", coordinates)

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)

TCS NQT 2023 coding Questions with codes hiringhustle 7


Given an array of pairs, find all symmetric
pairs in it

Problem Description:

Given an array of pairs, each representing a relationship between two


elements, the task is to find all pairs that are symmetric to each other.

A pair (a,b) is symmetric to another pair (c,d) if and only if a=d and b=c

Example:

Consider the following array of pairs:


Input:

pairs = [(1, 2), (3, 4), (2, 1), (4, 3), (5, 6)]

In this example, we have five pairs:

(1, 2)

(3, 4)

(2, 1)

(4, 3)

(5, 6)

Among these pairs, the symmetric pairs are:

(1, 2) symmetric to (2, 1)

(3, 4) symmetric to (4, 3)

Therefore, the output would be:

Symmetric pairs: [(1, 2), (2, 1), (3, 4), (4, 3)]

Explanation:

TCS NQT 2023 coding Questions with codes hiringhustle 8


In the input array, we observe that the pairs (1, 2) and (2, 1) are symmetric to
each other because the first element of the first pair is equal to the second
element of the second pair, and vice versa. Similarly, the pairs (3, 4) and (4, 3)
exhibit the same symmetry.
Test Cases:

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:

Symmetric pairs: [(4, 5), (5, 4)]

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 = {}

for pair in pairs:


if pair[::-1] in pair_dict:

TCS NQT 2023 coding Questions with codes hiringhustle 9


symmetric_pairs.append(pair)
else:
pair_dict[pair] = True

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)

Block swap algorithm for array rotation

Problem Statement:

TCS NQT 2023 coding Questions with codes hiringhustle 10


Given an array of integers arr and an integer d , implement a function to rotate
the array arr by d positions using the block swap algorithm.

Function Description:

def rotate_array(arr: List[int], d: int) -> List[int]:


"""
Rotate the given array by 'd' positions using the block
swap algorithm.

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.

1 ≤ len(arr) ≤ 1051 ≤ len(arr) ≤ 10power⁍

0 ≤ len(arr)0 ≤ d ≤ len(arr)

Test Case 1:
Input:

arr = [1, 2, 3, 4, 5, 6, 7]
d = 2

Expected Output:

TCS NQT 2023 coding Questions with codes hiringhustle 11


[3, 4, 5, 6, 7, 1, 2]

Test Case 2:
Input:

arr = [10, 20, 30, 40, 50, 60]


d = 3

Expected Output:

[40, 50, 60, 10, 20, 30]

Code:

from typing import List

def swap(arr, start_idx1, start_idx2, size):


for i in range(size):
arr[start_idx1 + i], arr[start_idx2 + i] = arr[star
t_idx2 + i], arr[start_idx1 + i]

def rotate_array(arr: List[int], d: int) -> List[int]:


n = len(arr)
if d == 0 or d == n:
return arr

# Handle the case where d > n


d = d % n

# Swap the elements of block A with block B


if d < n - d:
swap(arr, 0, n - d, d)
rotate_array(arr[:n - d], d)
else:
swap(arr, 0, d, n - d)
rotate_array(arr[n - d:], n - d)

TCS NQT 2023 coding Questions with codes hiringhustle 12


return arr

# 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)

Removing Non-Alphabetic Characters from a String


in Python
In this article, we'll explore how to remove all characters other than alphabets
from a given string using Python.

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.

Here's how the function works:

pythonCopy code
def remove_non_alphabets(input_string: str) -> str:

TCS NQT 2023 coding Questions with codes hiringhustle 13


"""
Remove all characters other than alphabets from the giv
en string.

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:

input_string = "Hello123, World!"


output = remove_non_alphabets(input_string)
print("Output:", output)

Expected Output:

"HelloWorld"

Test Case 2:

input_string = "The quick brown fox jumps over the lazy do


g."
output = remove_non_alphabets(input_string)
print("Output:", output)

TCS NQT 2023 coding Questions with codes hiringhustle 14


Expected Output:

"Thequickbrownfoxjumpsoverthelazydog"

Code in Python3

def remove_non_alphabets(input_string: str) -> str:


"""
Remove all characters other than alphabets from the giv
en string.

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

TCS NQT 2023 coding Questions with codes hiringhustle 15


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 dog."
output2 = remove_non_alphabets(input_string2)
print("Output for Test Case 2:", output2)

Kth largest factor of number N

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:

def kth_largest_factor(n: int, k: int) -> int:


"""
Find the kth largest factor of n.

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

TCS NQT 2023 coding Questions with codes hiringhustle 16


rwise -1.
"""
pass

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

TCS NQT 2023 coding Questions with codes hiringhustle 17


def kth_largest_factor(n: int, k: int) -> int:
factors = []
for i in range(1, n+1):
if n % i == 0:
factors.append(i)
factors.sort(reverse=True)
if k <= len(factors):
return factors[k-1]
else:
return -1

# 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)

TCS NQT 2023 coding Questions with codes hiringhustle 18

You might also like