Python Coding Interview Questions for SDET — Performance | Part 1
Hello everyone! As a Performance Engineer, I’ve used my personal interview experiences to compile a list of the most frequently asked coding interview questions for SDETs. Working through these questions should equip you to handle most of the coding challenges you may encounter in SDET interviews.
1.Swap values of two variables without direct reassignment and without creating any extra variables.
# Python code to swap two numbers
# using + and - operators
x = 5.4
y = 10.3
print ("Before swapping: ")
print("Value of x : ", x, " and y : ", y)
# Swap code
x = x + y # x = 15.7, y = 10.3
y = x - y # x = 15.7, y = 5.4
x = x - y # x = 10.3, y = 5.4
print ("After swapping: ")
print("Value of x : ", x, " and y : ", y)
2. String reverse. Write a method that will take one string as an argument and return the reverse version of this string.
def reverseString(inputString):
outputString = ""
for char in inputString:
outputString = char + outputString
return outputString
# Usage example:
inputString = "Peter"
reversedString = reverseString(inputString)
print(reversedString)
# Test cases
print(reversedString("apple")) # Output: elppa
print(reversedString("1234567")) # Output: 7654321
3.Number reverse. Write a method that will take number as an argument and return the reverse version of that number.
The straightforward solution would be to convert the number to a string and use the above approach. Some interviewers will not allow it. So let’s take a look at what we can do here.
def reverse_number(number):
reversed_number = 0
# Store Boolean Sign False to Preserve the sign of the number
negative = number < 0
# Make the number positive for digit reversal
if negative:
number = -number
while number != 0:
# Get the last digit
remainder = number % 10
# Remove the last digit from the number
number = number // 10
# Build the reversed number
reversed_number = reversed_number * 10 + remainder
# Restore the original sign to the reversed number
if negative:
reversed_number = -reversed_number
return reversed_number
# Example usage:
original_number = -586
reversed_number = reverse_number(original_number)
print("Original number:", original_number, "Reversed number:", reversed_number)
# Test cases
print("Reversed of 1234:", reverse_number(1234)) # Output: 4321
print("Reversed of -567:", reverse_number(-567)) # Output: -765
print("Reversed of 1200:", reverse_number(1200)) # Output: 21
print("Reversed of 9:", reverse_number(9)) # Output: 9
print("Reversed of 0:", reverse_number(0)) # Output: 0
4. Array reverse. Write a method that will take an array as an argument and reverse it.
def reverse_array(arr):
start = 0
end = len(arr) - 1
while start < end:
# Swap elements at start and end indices
arr[start], arr[end] = arr[end], arr[start]
# Move start pointer to the right and end pointer to the left
start, end = start + 1, end - 1
# Example usage:
array_list = [12.55, 15, 6.6, 0.4, 52.5, 55.5]
reverse_array(array_list)
print("Reversed array:", array_list)
5. Prime number Check: Write a method that checks if a given integer, is a prime number.
def prime_checker(num):
# Return False for non-prime cases
if num <= 1:
return False
# Return True for prime numbers 2 and 3
elif num <= 3:
return True
# Eliminate even numbers greater than 2 and multiples of 3
elif num % 2 == 0 or num % 3 == 0:
return False
else:
# Check from 5 up to the square root of num
"""
If 'num' is divisible by any number larger than its square root,
it must also be divisible by some number smaller than its square root
or
use 6n ± 1 method for a more optimized approach.
"""
for i in range(5, int(num**0.5) + 1, 2): # Only check odd numbers
if num % i == 0:
return False
return True
# Example usage:
num_to_check = 30
print(f"Is {num_to_check} a prime number? {prime_checker(num_to_check)}")
# Prime number checker test cases
print(prime_checker(2)) # Output: True
print(prime_checker(3)) # Output: True
print(prime_checker(29)) # Output: True
print(prime_checker(100)) # Output: False
print(prime_checker(0)) # Output: False
print(prime_checker(-7)) # Output: False