Python Interview Programs

30 Python programs asked in SDET and automation interviews, ordered from easy to hard. Each one has a worked example, hints and starter code; sign in to see the reference solution.

Easy (12)

  1. Check whether a number is prime

    Determine whether n is prime.

    Easy· Math ·10 min

  2. Check whether a string is a palindrome

    Determine whether a string reads identically from both ends.

    Easy· Strings ·10 min

  3. Check whether two strings are anagrams

    Determine whether two strings contain the same character multiset.

    Easy· Hashing ·10 min

  4. Compute the factorial of a number

    Compute n factorial exactly.

    Easy· Numbers ·10 min

  5. Count vowels and consonants

    Count the English vowels and consonants in a line.

    Easy· Strings ·10 min

  6. Find the first occurrence with binary search

    Return the first index of a target in a sorted array, or -1 if absent.

    Easy· Sorting & Searching ·10 min

  7. Find the missing number from zero through n

    Find the one missing integer from the inclusive range 0 through n.

    Easy· Arrays ·10 min

  8. Find the second largest distinct value

    Find the second largest distinct integer in an array.

    Easy· Arrays ·10 min

  9. Generate the Fibonacci series

    Print the first n Fibonacci numbers starting with 0 and 1.

    Easy· Numbers ·10 min

  10. Remove duplicate values while preserving order

    Return each array value once, in order of its first occurrence.

    Easy· Collections ·10 min

  11. Reverse a string

    Reverse the characters of the supplied string.

    Easy· Strings ·10 min

  12. Sum the digits of an integer

    Return the sum of the decimal digits of an integer.

    Easy· Numbers ·10 min

Medium (13)

  1. Compress consecutive character runs

    Encode each consecutive run as its character followed by its length.

    Medium· Strings ·20 min

  2. Count subarrays with a target sum

    Count nonempty contiguous subarrays whose sum equals a target.

    Medium· Hashing ·20 min

  3. Find a pair of indices with a target sum

    Return indices of two distinct array elements whose sum equals the target.

    Medium· Hashing ·20 min

  4. Find the first non-repeated character

    Return the first character that occurs exactly once, or NONE.

    Medium· Hashing ·20 min

  5. Find the longest common prefix

    Find the longest prefix shared by every supplied string.

    Medium· Strings ·20 min

  6. Find the maximum contiguous subarray sum

    Return the largest sum of a nonempty contiguous subarray.

    Medium· Dynamic Programming ·20 min

  7. Group anagrams in first-seen order

    Group lowercase words that are anagrams of each other.

    Medium· Collections ·20 min

  8. List all primes up to n

    Print all primes less than or equal to n in increasing order.

    Medium· Math ·20 min

  9. Merge two sorted arrays

    Merge two nondecreasing arrays into one sorted array.

    Medium· Two Pointers ·20 min

  10. Move all zeros to the end

    Move all zeros to the end of an array without changing nonzero order.

    Medium· Two Pointers ·20 min

  11. Rotate an array to the right

    Rotate an array right by k positions in place.

    Medium· Arrays ·20 min

  12. Traverse a matrix in spiral order

    Return matrix values in clockwise spiral order from the top-left cell.

    Medium· Matrices ·20 min

  13. Validate balanced brackets

    Determine whether a bracket string is correctly nested and balanced.

    Medium· Stack & Queue ·20 min

Hard (5)

  1. Compute the minimum edit distance

    Return the fewest single-character edits needed to transform one string into another.

    Hard· Dynamic Programming ·35 min

  2. Find the longest strictly increasing subsequence length

    Return the length of a longest strictly increasing subsequence.

    Hard· Dynamic Programming ·35 min

  3. Find the longest substring without repeated characters

    Return the maximum length of a substring containing no repeated characters.

    Hard· Sliding Window ·35 min

  4. Find the maximum in every fixed-size window

    Return the maximum value for every contiguous window of size k.

    Hard· Stack & Queue ·35 min

  5. Find the minimum covering substring

    Return the shortest substring of s containing every character of t with its required multiplicity.

    Hard· Sliding Window ·35 min