Python Interview Questions - Aninexus

Python 3.13 Interview Guide: 5 Essential Questions for New Grads

Python is one of the most in-demand programming languages for software development, data science, and automation roles.

Recruiters frequently use Python coding challenges to filter candidates quickly, so having clear, optimized solutions ready can make a strong first impression.

1. Find the First Duplicate in a List

In interviews, this question assesses your understanding of time complexity and the use of data structures, such as sets. The goal is to scan the list and return the first element that appears more than once.

Problem

Given a list of integers, return the first duplicate value you encounter while scanning from left to right.
Example:
Input: [3, 1, 3, 4, 2]
Output: 3

Python solution

pythondef first_duplicate(lst):
    seen = set()
    for x in lst:
        if x in seen:
            return x
        seen.add(x)
    return None

print(first_duplicate([3, 1, 3, 4, 2]))  # Output: 3

How to explain this in an interview

  • You maintain a set called seen to track numbers you’ve already encountered.
  • For each element:
    • If it is already in seen That is the first duplicate, so you return it.
    • If not, you add it to seen and continue.
  • Time complexity: O(n)O(n) because each element is checked once.
  • Space complexity: O(n)O(n) in the worst case (if there are no duplicates).

This demonstrates to the interviewer that you understand both code clarity and performance, which are essential in real-world Python applications.


2. Check Whether a Number Is a Palindrome

Palindrome questions are extremely common in Python interview questions because they test your string manipulation and logical thinking. A palindrome is a number (or string) that reads the same forward and backward.

Problem

Check whether a given number is a palindrome.
Examples:

  • 121 → True
  • 123 → False

Python solution

pythondef is_pal_num(n):
    return str(n) == str(n)[::-1]

print(is_pal_num(121))  # True
print(is_pal_num(123))  # False

How to explain this in an interview

  • Convert the number to a string using str(n).
  • Use Python slicing [::-1] to reverse the string.
  • Compare the original string with its reversed version:
    • If they match, the number is a palindrome.
    • If not, it is not a palindrome.
  • This solution is concise, readable, and highlights Python’s powerful slicing features.

You can also mention an alternative numeric (non-string) approach if the interviewer wants a solution without converting to a string.


3. Sort a Dictionary by Its Values

Sorting dictionaries by values is a common real-world task in Python, especially in data processing and analytics code. Python interview questions in this area test your understanding of higher-order functions and dictionary handling.

Problem

Sort a dictionary by its values in ascending order and return a new dictionary.

Python solution

pythondef sort_by_value(d):
    return dict(sorted(d.items(), key=lambda x: x[1]))

print(sort_by_value({'a': 3, 'b': 1, 'c': 2}))
# Output: {'b': 1, 'c': 2, 'a': 3}

How to explain this in an interview

  • d.items() returns key–value pairs as tuples.
  • sorted(..., key=lambda x: x[1]) tells Python to sort these pairs based on the value (x[1]).
  • Wrapping the result in dict(...) converts the sorted list of tuples back into a dictionary.
  • This demonstrates:
    • Understanding of sorted()
    • Lambda functions
    • Dictionary transformations

You can also mention that to sort in descending order, you can add reverse=True inside sorted().


4. Return All Prime Numbers Up to N

Prime number questions are classic Python interview questions that show your understanding of loops, mathematics, and optimization. Here, you must generate all prime numbers up to a given number n.

Problem

Given an integer n, return a list of all prime numbers from 2 to n (inclusive).
Example:
Input: 10
Output: [2, 3, 5, 7]

Python solution

pythondef primes_upto(n):
    primes = []
    for num in range(2, n + 1):
        for i in range(2, int(num**0.5) + 1):
            if num % i == 0:
                break
        else:
            primes.append(num)
    return primes

print(primes_upto(10))  # [2, 3, 5, 7]

How to explain this in an interview

  • You loop from 2 to n.
  • For each number num, you try dividing it by every integer i from 2 to sqrt(num):
    • If num % i == 0, the number is not prime, so break the loop.
  • The for/else construct:
    • The else block runs only if the inner loop does not encounter a break, meaning num is prime.
  • Using int(num**0.5) + 1 reduces the number of checks, which is more efficient than checking up to num - 1.

You can also mention that for very large n, algorithms like the Sieve of Eratosthenes are more efficient and commonly tested in advanced Python interviews.


5. Convert a List of Numbers into a String

String and list manipulation is at the heart of many Python interview questions. This one checks whether you can cleanly convert numeric data into a string representation.

Problem

Given a list of numbers, convert it into one continuous string.
Example:
Input: [1, 2, 3]
Output: "123"

Python solution

pythondef list_to_string(lst):
    return "".join(map(str, lst))

print(list_to_string([1, 2, 3]))  # Output: 123

How to explain this in an interview

  • map(str, lst) : convert each integer in the list to a string.
  • "".join(...) concatenates all string elements without any separator.
  • This is far more efficient and Pythonic than concatenating with + in a loop.
  • If you want a separator (like commas or spaces), you can use:
    • " ".join(map(str, lst)) → "1 2 3"
    • ",".join(map(str, lst)) → "1,2,3"

This demonstrates familiarity with core Python features that frequently appear in coding interview tasks.

Leave a Reply

Your email address will not be published. Required fields are marked *