Most asked top Interview Questions and Answers & Online Test
Education platform for interview prep, online tests, tutorials, and live practice

Build skills with focused learning paths, mock tests, and interview-ready content.

WithoutBook brings subject-wise interview questions, online practice tests, tutorials, and comparison guides into one responsive learning workspace.

Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Python Coding Interview Questions and Answers

Ques 16. Write a Python program to find the length of the longest increasing subsequence in an array.

def longest_increasing_subsequence(arr):

  n = len(arr)\n lis = [1]*n

  for i in range(1, n):

    for j in range(0, i):

      if arr[i] > arr[j] and lis[i] < lis[j] + 1:

        lis[i] = lis[j] + 1

          return max(lis)

Example:

longest_increasing_subsequence([10, 22, 9, 33, 21, 50, 41, 60, 80])  # Output: 6

Is it helpful? Add Comment View Comments
 

Ques 17. Create a Python generator function to generate the power set of a given set.

def power_set(input_set):

  n = len(input_set)

  for i in range(1 << n):

    subset = [input_set[j]

      for j in range(n):

        if (i & (1 << j)) > 0]

          yield subset

Example:

power_set({1, 2, 3})  # Output: [], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]

Is it helpful? Add Comment View Comments
 

Ques 18. Write a Python function to implement the binary search algorithm.

def binary_search(arr, target):

  low, high = 0, len(arr) - 1

  while low <= high:

    mid = (low + high) // 2

    if arr[mid] == target:

      return mid

    elif arr[mid] < target:

      low = mid + 1

    else:

      high = mid - 1

    return -1

Example:

binary_search([1, 2, 3, 4, 5, 6, 7], 4)  # Output: 3

Is it helpful? Add Comment View Comments
 

Ques 19. Implement a function to check if a binary tree is balanced.

class TreeNode:
 def __init__(self, value):
 self.value = value
 self.left = None
 self.right = None


def is_balanced(root):
 if root is None:
 return True
 left_height = height(root.left)
 right_height = height(root.right)
 return abs(left_height - right_height) <= 1 and is_balanced(root.left) and is_balanced(root.right)


def height(node):
 if node is None:
 return 0
 return max(height(node.left), height(node.right)) + 1

Example:

# Example usage: Check if 'root' is a balanced binary tree
is_balanced(root)

Is it helpful? Add Comment View Comments
 

Ques 20. Write a Python program to implement a simple LRU (Least Recently Used) cache.

from collections import OrderedDict

class LRUCache:
 def __init__(self, capacity):
 self.cache = OrderedDict()
 self.capacity = capacity

 def get(self, key):
 if key in self.cache:
 self.cache.move_to_end(key)
 return self.cache[key]
 return -1

 def put(self, key, value):
 if len(self.cache) >= self.capacity:
 self.cache.popitem(last=False)
 self.cache[key] = value
 self.cache.move_to_end(key)

Example:

# Example usage:
lru_cache = LRUCache(3)
lru_cache.put(1, 1)
lru_cache.put(2, 2)
lru_cache.put(3, 3)
print(lru_cache.get(2))  # Output: 2

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

Copyright © 2026, WithoutBook.