JOBSEEKER?

# Python Interview Questions

##### Practice for Python interviews by solving TestDome questions. Our interview questions are used by more than 7,000 companies and 450,000 individual test takers.

Companies: Use Our Tests for Screening

Need to practice your Python programming skills for an upcoming job interview? Try solving these Python interview questions that test knowledge of lists, iteration, recursion, sorting, and other skills. We’ll provide feedback on your answers, and you can use a hint if you get stuck.

These Python interview questions are examples of real tasks used by employers to screen job candidates such as Python developers, back-end developers, full-stack developers, and others that require knowledge of the Python programming language and its native data structures and language constructs.

## 1. File Owners

Dictionary

Easy

Implement a group_by_owners function that:

• Accepts a dictionary containing the file owner name for each file name.
• Returns a dictionary containing a list of file names for each owner name, in any order.

For example, for dictionary {'Input.txt': 'Randy', 'Code.py': 'Stan', 'Output.txt': 'Randy'} the group_by_owners function should return {'Randy': ['Input.txt', 'Output.txt'], 'Stan': ['Code.py']}.

Python 3.7.4

•   Each owner has a single file: Wrong answer
•   Function group_by_owners is called more than one time: Wrong answer

Arithmetic

Easy

Implement the function find_roots to find the roots of the quadratic equation: ax2 + bx + c = 0. The function should return a tuple containing roots in any order. If the equation has only one solution, the function should return that solution as both elements of the tuple. The equation will always have at least one solution.

The roots of the quadratic equation can be found with the following formula: For example, find_roots(2, 10, 8) should return (-1, -4) or (-4, -1) as the roots of the equation 2x2 + 10x + 8 = 0 are -1 and -4.

Python 3.7.4

## 3. Binary Search Tree

Algorithmic thinking Recursion Tree

Easy Binary search tree (BST) is a binary tree where the value of each node is larger or equal to the values in all the nodes in that node's left subtree and is smaller than the values in all the nodes in that node's right subtree.

Write a function that, efficiently with respect to time used, checks if a given binary search tree contains a given value.

For example, for the following tree:

• n1 (Value: 1, Left: null, Right: null)
• n2 (Value: 2, Left: n1, Right: n3)
• n3 (Value: 3, Left: null, Right: null)

Call to contains(n2, 3) should return True since a tree with root at n2 contains number 3.

Python 3.7.4

•   Performance test on a large tree: Wrong answer

## 4. Song

Easy

A playlist is considered a repeating playlist if any of the songs contain a reference to a previous song in the playlist. Otherwise, the playlist will end with the last song which points to None.

Implement a function is_repeating_playlist that, efficiently with respect to time used, returns true if a playlist is repeating or false if it is not.

For example, the following code prints "True" as both songs point to each other.

``````first = Song("Hello")
second = Song("Eye of the tiger")

first.next_song(second)
second.next_song(first)

print(first.is_repeating_playlist())``````
Python 3.7.4

•   If playlist repeats, last song is followed by first song: Wrong answer
•   If playlist repeats, starting songs can be skipped: Wrong answer
•   Performance test on a large playlist: Wrong answer

## 5. Two Sum

Algorithmic thinking Dictionary

Easy

Write a function that, when passed a list and a target sum, returns, efficiently with respect to time used, two distinct zero-based indices of any two of the numbers, whose sum is equal to the target sum. If there are no two numbers, the function should return None.

For example, find_two_sum([3, 1, 5, 7, 5, 9], 10) should return a single tuple containing any of the following pairs of indices:

• 0 and 3 (or 3 and 0) as 3 + 7 = 10
• 1 and 5 (or 5 and 1) as 1 + 9 = 10
• 2 and 4 (or 4 and 2) as 5 + 5 = 10
Python 3.7.4

•   Distinct numbers with and without solutions: Wrong answer
•   Duplicate numbers with and without solutions: Wrong answer
•   Performance test with a large list of numbers: Wrong answer

## 6. League Table

Collections Sorting

Hard

The LeagueTable class tracks the score of each player in a league. After each game, the player records their score with the record_result function.

The player's rank in the league is calculated using the following logic:

1. The player with the highest score is ranked first (rank 1). The player with the lowest score is ranked last.
2. If two players are tied on score, then the player who has played the fewest games is ranked higher.
3. If two players are tied on score and number of games played, then the player who was first in the list of players is ranked higher.

Implement the player_rank function that returns the player at the given rank.

For example:

``````table = LeagueTable(['Mike', 'Chris', 'Arnold'])
table.record_result('Mike', 2)
table.record_result('Mike', 3)
table.record_result('Arnold', 5)
table.record_result('Chris', 5)
print(table.player_rank(1))``````

All players have the same score. However, Arnold and Chris have played fewer games than Mike, and as Chris is before Arnold in the list of players, he is ranked first. Therefore, the code above should display "Chris".

Python 3.7.4

•   Players have different scores: Wrong answer
•   Players tied by score: Wrong answer
•   Players tied by games played: Wrong answer

## 7. Sorted Search

Algorithmic thinking Binary search

Hard

Implement function count_numbers that accepts a sorted list of unique integers and, efficiently with respect to time used, counts the number of list elements that are less than the parameter less_than.

For example, count_numbers([1, 3, 5, 7], 4) should return 2 because there are two list elements less than 4.

Python 3.7.4

•   Various small lists: Wrong answer
•   Performance test when sorted_list contains less_than: Wrong answer
•   Performance test when sorted_list doesn't contain less_than: Wrong answer

## 8. Train Composition

Algorithmic thinking Deque

Hard

A TrainComposition is built by attaching and detaching wagons from the left and the right sides, efficiently with respect to time used.

For example, if we start by attaching wagon 7 from the left followed by attaching wagon 13, again from the left, we get a composition of two wagons (13 and 7 from left to right). Now the first wagon that can be detached from the right is 7 and the first that can be detached from the left is 13.

Implement a TrainComposition that models this problem. Python 3.7.4

•   Performance test with a large number of wagons: Wrong answer

## 9. Route Planner

2D array Algorithmic thinking Graphs

Hard

As a part of the route planner, the route_exists method is used as a quick filter if the destination is reachable, before using more computationally intensive procedures for finding the optimal route.

The roads on the map are rasterized and produce a matrix of boolean values - True if the road is present or False if it is not. The roads in the matrix are connected only if the road is immediately left, right, below or above it.

Finish the route_exists method so that it returns True if the destination is reachable or False if it is not. The from_row and from_column parameters are the starting row and column in the map_matrix. The to_row and to_column are the destination row and column in the map_matrix. The map_matrix parameter is the above mentioned matrix produced from the map.

For example, for the given rasterized map, the code below should return True since the destination is reachable: ``````map_matrix = [
[True, False, False],
[True, True, False],
[False, True, True]
];

route_exists(0, 0, 2, 2, map_matrix)``````
Python 3.7.4

•   Various routes from top-left to bottom-right corner: Wrong answer
•   Various routes, starts, and destinations: Wrong answer
•   Performance test on large map: Wrong answer

## 10. Ice Cream Machine

Iteration Lists

Easy

Implement the IceCreamMachine's scoops method so that it returns all combinations of one ingredient and one topping. If there are no ingredients or toppings, the method should return an empty list.

For example, IceCreamMachine(["vanilla", "chocolate"], ["chocolate sauce"]).scoops() should return [['vanilla', 'chocolate sauce'], ['chocolate', 'chocolate sauce']].

Python 3.7.4

•   Various ingredients and one topping: Wrong answer
•   Various ingredients and toppings: Wrong answer
•   No ingredients and no toppings: Wrong answer

## 11. Merge Names

Lists

Easy

Implement the unique_names method. When passed two lists of names, it will return a list containing the names that appear in either or both lists. The returned list should have no duplicates.

For example, calling unique_names(['Ava', 'Emma', 'Olivia'], ['Olivia', 'Sophia', 'Emma']) should return a list containing Ava, Emma, Olivia, and Sophia in any order.

Python 3.7.4

•   Each array has distinct names: Wrong answer
•   Each array has duplicate names: Wrong answer
•   Arrays have some names in common: Wrong answer

## 12. Pipeline

Closures Higher order function

Hard

As part of a data processing pipeline, complete the implementation of the pipeline method:

• The method should accept a variable number of functions, and it should return a new function that accepts one parameter arg.
• The returned function should call the first function in the pipeline with the parameter arg, and call the second function with the result of the first function.
• The returned function should continue calling each function in the pipeline in order, following the same pattern, and return the value from the last function.

For example, pipeline(lambda x: x * 3, lambda x: x + 1, lambda x: x / 2) then calling the returned function with 3 should return 5.0.

Python 3.7.4

•   Various data types: Wrong answer

If you feel ready, take one of our timed public Python Interview Questions tests: Python Online Test (Easy / Hard)

Python Algorithms Online Test (Easy / Hard)

Python Algorithms and SQL Online Test (Easy / Hard)

Python and SQL Online Test (Easy / Hard) HTML/CSS, JavaScript, Python Algorithms, and SQL Online Test (Easy / Hard)

HTML/CSS, JavaScript, Python, and SQL Online Test (Easy / Hard) General and Python Data Science, Python, and SQL Online Test (Easy / Hard)

Not exactly what you are looking for? Go to our For Jobseekers section.