Give 30 medium Python coding problems with brief solutions

Here are 30 medium-level Python coding problems along with brief solutions to help you practice effectively:

PYTHON DEVELOPER

  1. Find the first non-repeating character in a string
    Use a hash map to count character frequency and then find the first with count 1.

  2. Merge two sorted linked lists
    Use two pointers to merge lists in O(n) time.

  3. Implement binary search on a sorted array
    Classic divide and conquer approach with mid index comparison.

  4. Convert Roman numerals to integer
    Map symbols to values and add/subtract based on neighbor comparison.

  5. Detect cycle in a linked list
    Use Floyd’s Tortoise and Hare algorithm.

  6. Longest substring without repeating characters
    Sliding window with hash set for tracking characters.

  7. Find all pairs in array that sum to a target
    Use a hash map to store complements while iterating.

  8. Group anagrams from a list of strings
    Sort each string as key in a dictionary grouping anagrams.

  9. Find kth largest element in an array
    Use a min-heap of size k or quickselect algorithm.

  10. Valid parentheses checker
    Use a stack to match opening and closing braces.

  11. Implement a stack with getMin operation in O(1)
    Maintain an auxiliary stack to track current minimum.

  12. Product of array except self
    Calculate prefix and suffix products without division.

  13. Rotate array to the right by k steps
    Reverse whole array, then reverse first k and last n-k elements.

  14. Search in a rotated sorted array
    Modified binary search checking rotated halves.

  15. Intersection of two arrays
    Use sets and find common elements.

  16. Find duplicate numbers in array
    Use a set to track seen or modify array indices.

  17. Number of islands in a grid
    Use DFS or BFS to count connected components of 1s.

  18. Implement Trie (Prefix Tree)
    Use nested dictionaries or classes for nodes.

  19. Evaluate Reverse Polish Notation
    Use a stack to evaluate based on operators and operands.

  20. Find all permutations of a string
    Use backtracking to generate permutations.

  21. Median of two sorted arrays
    Use binary search to partition arrays optimally.

  22. Coin change - minimum coins
    Dynamic programming approach with bottom-up table.

  23. Calculate power(x, n)
    Use fast exponentiation (divide-and-conquer).

  24. Longest Palindromic Substring
    Expand around centers for O(n²) solution.

  25. Clone a graph
    DFS or BFS with hashmap to copy nodes.

  26. Binary tree level order traversal
    BFS using queue to collect nodes level-wise.

  27. Maximum subarray sum
    Kadane’s algorithm with linear scan.

  28. Merge intervals
    Sort intervals and merge overlapping pairs.

  29. Design a parking lot system (OOP)
    Model entities and manage spots logically.

  30. Implement LRU Cache
    Use OrderedDict or combination of hashmap + doubly linked list.

These problems cover widely used algorithmic patterns such as sliding windows, hashing, heaps, recursion/backtracking, dynamic programming, and graph/tree traversals, useful for medium-level Python interview rounds.