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. Student Majors**

*Public questions*(free account) are common interview questions. They are great for practicing, or if you want to filter candidates using the classic problems.

A university's Office of Admission keeps track of student majors. Each student can have a single major. Below is an example of how their system stores students, majors, and how it manipulates them:

```
students = [("Allen Anderson", "Computer Science"),
("Edgar Einstein", "Engineering"),
("Farrah Finn", "Fine Arts")]
def add_new_student(students, name, major):
students.append((name, major))
def update_student(students, index, name, major):
students[index] = (name, major)
def find_students_by_name(students, name):
return [student for student in students if name in student[0]]
def get_all_majors(students):
return [student[1] for student in students]
```

What can be concluded from the code snippet above?

*(Select all acceptable answers.)*

**2. File Owners**

*Public questions*(free account) are common interview questions. They are great for practicing, or if you want to filter candidates using the classic problems.

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']}*.

**3. Quadratic Equation**

*Public questions*(free account) are common interview questions. They are great for practicing, or if you want to filter candidates using the classic problems.

Implement the function *find_roots* to find the roots of the quadratic equation: ax^{2} + 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 2x^{2} + 10x + 8 = 0 are -1 and -4.

**4. Binary Search Tree**

*Public questions*(free account) are common interview questions. They are great for practicing, or if you want to filter candidates using the classic problems.

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.

**5. Song**

*Public questions*(free account) are common interview questions. They are great for practicing, or if you want to filter candidates using the classic problems.

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

**6. Two Sum**

*Public questions*(free account) are common interview questions. They are great for practicing, or if you want to filter candidates using the classic problems.

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

**7. League Table**

*Public questions*(free account) are common interview questions. They are great for practicing, or if you want to filter candidates using the classic problems.

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:

- The player with the highest score is ranked first (rank 1). The player with the lowest score is ranked last.
- If two players are tied on score, then the player who has played the fewest games is ranked higher.
- 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".

**8. Sorted Search**

*Public questions*(free account) are common interview questions. They are great for practicing, or if you want to filter candidates using the classic problems.

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.

**9. Train Composition**

*Public questions*(free account) are common interview questions. They are great for practicing, or if you want to filter candidates using the classic problems.

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.

**10. Route Planner**

*Public questions*(free account) are common interview questions. They are great for practicing, or if you want to filter candidates using the classic problems.

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

**11. Ice Cream Machine**

*Public questions*(free account) are common interview questions. They are great for practicing, or if you want to filter candidates using the classic problems.

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']].

**12. Merge Names**

*Public questions*(free account) are common interview questions. They are great for practicing, or if you want to filter candidates using the classic problems.

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.

**13. Pipeline**

*Public questions*(free account) are common interview questions. They are great for practicing, or if you want to filter candidates using the classic problems.

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.