Python Interview Questions

Want to become an expert in cracking Python interview questions?

Start with practicing the questions below. Whether a question involves multiple choice or live coding, we will give you hints as you go and tell you if your answers are correct or incorrect.

After that, take our timed public Python Interview Questions Test.

To use our service for testing candidates, buy a pack of candidates.


1. File Owners

Python Language Public

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

Easy  
10min
Python 3.6.5
 


  •   Example case: Wrong answer
  •   Each owner has a single file: Wrong answer
  •   Various files: Wrong answer


2. Ice Cream Machine

Python Collections Public New

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

Easy 
10min
Python 3.6.5
 


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


3. Merge Names

Python Arrays Public New

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

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

Easy 
10min
Python 3.6.5
 


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


4. Palindrome

Python Strings Public

A palindrome is a word that reads the same backward or forward.

Write a function that checks if a given word is a palindrome. Character case should be ignored.

For example, is_palindrome("Deleveled") should return True as character case should be ignored, resulting in "deleveled", which is a palindrome since it reads the same backward and forward.

Easy  
10min
Python 3.6.5
 


  •   Example case: Wrong answer
  •   Lowercase words: Wrong answer
  •   Various words: Wrong answer


5. Binary Search Tree

Python Algorithmic thinking Data structures Public

A three-node binary tree.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.

Easy  
15min
Python 3.6.5
 


  •   Example case: Wrong answer
  •   Correctness: Wrong answer
  •   Performance test on a large tree: Wrong answer


6. Playlist

Python Algorithmic thinking Searching Public

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())
Easy  
15min
Python 3.6.5
 


  •   Example case: Wrong answer
  •   Song will repeat: Wrong answer
  •   Song will not repeat: Wrong answer
  •   Performance test on a large playlist: Wrong answer


7. Two Sum

Python Algorithmic thinking Searching Public New

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
Easy 
30min
Python 3.6.5
 


  •   Example case: Wrong answer
  •   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


8. Pipeline

Python Closures Public New

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.pipeline(lambda x: x * 3, lambda x: x + 1, lambda x: x / 2) then calling the returned function with 3 should return 5.0.

Hard 
10min
Python 3.6.5
 


  •   Example case: Wrong answer
  •   Various functions: Wrong answer
  •   Various data types: Wrong answer


9. League Table

Python Collections Sorting Public New

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".

Hard  
20min
Python 3.6.5
 


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


10. Path

Python Data structures Strings Public

Write a function that provides change directory (cd) function for an abstract file system.

Notes:

  • Root path is '/'.
  • Path separator is '/'.
  • Parent directory is addressable as '..'.
  • Directory names consist only of English alphabet letters (A-Z and a-z).
  • The function should support both relative and absolute paths.
  • The function will not be passed any invalid paths.
  • Do not use built-in path-related functions.

For example:

path = Path('/a/b/c/d')
path.cd('../x')
print(path.current_path)

should display '/a/b/c/x'.

Hard  
30min
Python 3.6.5
 


  •   Example case: Wrong answer
  •   Selecting child directories: Wrong answer
  •   Selecting parent directories: Wrong answer
  •   Selecting complex paths: Wrong answer


If you feel ready, take one of our timed public Python Interview Questions tests:
  • HTML/CSS, JavaScript, Python Algorithms, and SQL Online Test (Easy / Hard)
  • HTML/CSS, JavaScript, Python, and SQL Online Test (Easy / Hard)
  • Python Online Test (Easy / Hard)
  • Python Algorithms Online Test (Easy)
  • Python Algorithms and SQL Online Test (Easy)
  • Python and SQL Online Test (Easy / Hard)
Not exactly what you are looking for? Go to our For Jobseekers section.