DEV Community

AheadKit
AheadKit

Posted on

3 Python Programs Interviewers Ask Again and Again (With Real Outputs)

Interviewers are not as original as they pretend. Across companies, the same small programs keep coming back, because each one exposes a specific habit: how you loop, how you handle edge cases, and whether you know more than one way to solve a simple problem.

These three are from Python Interview Prep, a practice book of 140 programs and 100 interview questions where every program is printed with the real output it produces. Here they are exactly as they appear in the book.

Program 20 — Check Whether a Number Is Prime

What the interviewer is testing: Whether you understand what makes a number prime and can test for divisors in a sensible way. The detail they listen for: you only need to check divisors up to the square root.

number = 29

is_prime = True
if number < 2:
    is_prime = False
else:
    for i in range(2, int(number ** 0.5) + 1):
        if number % i == 0:
            is_prime = False
            break

if is_prime:
    print(number, "is a prime number.")
else:
    print(number, "is not a prime number.")
Enter fullscreen mode Exit fullscreen mode

Output:

29 is a prime number.
Enter fullscreen mode Exit fullscreen mode

Program 22 — Find the Factorial of a Number With a Loop

What the interviewer is testing: Whether you can build up a running product step by step inside a loop — and whether you start the result at 1, not 0 (start at 0 and the whole answer collapses to 0).

number = 6

result = 1
for i in range(1, number + 1):
    result = result * i

print(f"Factorial of {number} is {result}")
Enter fullscreen mode Exit fullscreen mode

Output:

Factorial of 6 is 720
Enter fullscreen mode Exit fullscreen mode

Program 33 — Reverse a String Three Ways

What the interviewer is testing: Whether you know more than one way to reverse text, since interviewers often follow up with "now do it without slicing."

text = "interview"

# Way 1: slicing with a step of -1
way1 = text[::-1]

# Way 2: reversed() joined back into a string
way2 = "".join(reversed(text))

# Way 3: build it with a loop
way3 = ""
for char in text:
    way3 = char + way3

print("Original:", text)
print("Using slicing:", way1)
print("Using reversed():", way2)
print("Using a loop:", way3)
Enter fullscreen mode Exit fullscreen mode

Output:

Original: interview
Using slicing: weivretni
Using reversed(): weivretni
Using a loop: weivretni
Enter fullscreen mode Exit fullscreen mode

Practise the rest free

The first 25 programs of the book, plus 10 of the interview questions, are free to read online with no email needed: aheadkit.me/free-chapter/python-interview-prep

The full book (135 pages, $7.99) covers all 140 programs — strings, lists, dictionaries, OOP, decorators, generators, file handling, threads, async and algorithm problems — plus 100 questions and a 7-day practice plan: Python Interview Prep on AheadKit

One honest note: these programs assume you already know variables, loops and functions. If you are starting Python from zero, learn the basics first, then use a set like this to sharpen up for the interview room.

Top comments (0)