No results found for the specified position. 11 Python Beginner Level Implementation Interview Questions

MockQuestions

Python Beginner Level Implementation Mock Interview

To help you prepare for your Python Beginner Level Implementation interview, here are 11 interview questions and answer examples.

Get More Information About Our Python Beginner Level Implementation Interview Questions

Question 1 of 11

Validate a square.

This interview question shows a developer's knowledge of Python functions.

You are given four points in 2-D space. Return true if they form a square, false otherwise. The points are integer coordinates and are given in any order.

Valid square- A quadrilateral with all sides equal and both diagonal equal. Also, each interior angle is equal to 90°.

/*Example*/

Input- [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Expected output- true

Input- [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12]
Expected output- false

Solution:

For four points, we can find distances between them. There are a total of 6 distances possible. To be a valid square, 4 of them should be equal (the sides) and 2 should be equal (diagonals), and satisfies the following condition (Pythagoras theorem)-
diagonal2 = 2 * side2
Instead of storing actual sides or diagonal, we are storing their square to avoid precision loss due to imperfect squares.

from collections import defaultdict

def distance(p, q):
	'''
		function returns the square of the distance 
		bw two point p and q
'''
	delta_x = p[0] - q[0]
	delta_y = p[1] - q[1]
	return (delta_x ** 2) + (delta_y ** 2)

def isValidSquare(p1, p2, p3, p4):
	'''
		1. Out of 6 distance combination 2 of one type (diag)
		   and 4 of one type (diag)
		2. side_sq * 2 = diag_square 
'''
	points = [p1, p2, p3, p4]	# all points in one iterable
	distances = []			# to record dist in btw all points

	# for 6 diff combinations
	for i in range(3):			
		for j in range(i + 1,  4):
			distances.append(distance(points[i], points[j]))

	# count occurance of all distances in distances array 
	# and count their frequencies (how many time each value occur)
	counts = defaultdict(int)
	for ele in distances:
		counts[ele] += 1
	
	# only two distinct distance (side and diag) else return false
	if len(counts) != 2:
		return False

	# iterate and get the side and diagonal actual value
	side_square = diag_square = None
	for key, value in counts.items():
		# break out of loop as soon we get both values
		if side_square and diag_square:
			break

		if value == 2:		# diag have freq of two
			diag_square = key
		elif value == 4:		# side have freq of four
			side_square = key

	# sq of diag is equal to 2 times sq of sides
	if side_square * 2 == diag_square:
		return True
	return False 

Time complexity- O(1)
Space complexity- O(1)

Written by on May 21st, 2021

Next Question

11 Python Beginner Level Implementation Interview Questions & Answers

Below is a list of our Python Beginner Level Implementation interview questions. Click on any interview question to view our answer advice and answer examples. You may view 5 answer examples before our paywall loads. Afterwards, you'll be asked to upgrade to view the rest of our answers.

  • 1. Validate a square.

  • 2. Format a string by grouping characters found in the string into similar lengths.

  • 3. How do you find two integers in the array such that their sum is 'target'?

  • 4. How do you determine if a string is valid or not?

  • 5. How can you find the third largest number in an array?

  • 6. Find if the array contains a unique number of occurrences for each value in the array or not.

  • 7. Create a running sum of a 1-D array.

  • 8. Determine if str1 is a subsequence of str2.

  • 9. Find the largest two numbers in an array without sorting them.

  • 10. Find unique vanishing numbers.

  • 11. Find if the robot returns to its home after given instructions or not.