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 developers, purchase one of the paid plans for companies.


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
Solve question

2. 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
Solve question

3. 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
Solve question

4. 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
Solve question

If you feel ready, take one of our timed public Python interview questions tests:
  • HTML/CSS, JavaScript, Python, and SQL online test ( Easy  /  Hard )
  • Python online test ( Easy )
  • Python and SQL online test ( Easy  /  Hard )
Not exactly what you are looking for? Go to our For developers section.