Login / Register |
5 Years SDET Role Specialization with Data Testing in Mphasis
#python #sdet #sql #mphasis
Posted by TechElliptica at 08 May 2025 ( Day(s) Before)

Rotate a given N x N matrix 90 degrees clockwise in-place without using any in-build function


input matrix =

[[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]


# Output

rotate_matrix =

[[7, 4, 1],

[8, 5, 2],

[9, 6, 3]]



This is quite a tricky question, as matrix questions always is. But there are 2 steps to solve this


1 - Transpose the matrix. We are making all row into columns and column into rows

2 - Reverse each row - We will reverse each row



Please find the code for rotation below


def rotate_matrix(matrix):
n = len(matrix)

# Step 1: Transpose the matrix
for i in range(n):
for j in range(i, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

# Step 2: Reverse each row manually
for i in range(n):
start = 0
end = n - 1
while start < end:
matrix[i][start], matrix[i][end] = matrix[i][end], matrix[i][start]
start += 1
end -= 1

return matrix

matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

rotate_matrix(matrix)
print(matrix)


While you are writing this type of response. always speak your logic while writing each line




Write a generator function that yields prime numbers up to 10.

In Python, a generator function is a special function that allows you to yield values one at a time, instead of returning them all at once like a regular function. This makes generators memory-efficient and ideal for handling large datasets or streams of data.


Key Characteristics of a generator function is

  1. Use yield instead of return.
  2. State is saved between yields — it resumes where it left off.
  3. Produces a generator object, which can be iterated using for loops or next().


Below is the code for prime_number generator function


def prime_generator():
for num in range(2, 11):
is_prime = True
for i in range(2, num-1):
if num % i == 0:
is_prime = False
break
if is_prime:
yield num

for prime in prime_generator():
print(prime)

Write a query solution to swap the seat id of every two consecutive students. If the number of students is odd, the id of the last student is not swapped. Return the result table ordered by id in ascending order. swap the seat id of every two consecutive students, ensuring the last student's ID remains unchanged if the total number of students is odd


Input Table - Seat Table


id

student

1

Abott

2

Doris

3

Emerson

4

Green

5

James



Output Table :


id

student

1

Doris

2

Abbot

3

Green

4

Emerson

5

James


This solution is a combination of CASE keyword and ORDER BY keyword along with your understanding with aggregate functions.


we can design our query like below


SELECT
CASE
WHEN id % 2 = 1 AND id + 1 <= (SELECT COUNT(*) FROM Seat) THEN id + 1
WHEN id % 2 = 0 THEN id - 1
ELSE id
END AS id,
student
FROM Seat
ORDER BY id;

In a source table we have date and timezone


date

timezone

24-04-2024 14:15:00

PST

24-04-2024 23:00:00

EST


Write a query to insert data in new table with below condition

if timezone is PST then add 6 hours

if timezone is EST then add 5 hours



Result:


date

24-04-2024 20:15:00

25-04-2024 04:00:00




In this question , Interviewer trying to understand your knowledge with date function.


for this question, we will use CASE and DATEADD function in SQL.


INSERT INTO TRGT(date)
SELECT
CASE
WHEN timezone = 'PST' THEN DATEADD(HOUR, 6, date)
WHEN timezone = 'EST' THEN DATEADD(HOUR, 5, date)
END AS date
FROM SRC;