Skip to content

Top 30 Python Coding QNA

Praveen Kumar Anwla edited this page Jan 30, 2024 · 12 revisions

Certainly! Here's a list of 30+ Python coding interview questions along with brief answers. Note that the answers provided are concise; during an interview, you may be expected to explain your thought process and approach more thoroughly.

  1. Reverse a String:

    s = "hello"
    reversed_s = s[::-1]
  2. Check for Palindrome:

    def is_palindrome(s):
        return s == s[::-1]
  3. Find the First Non-Repeated Character in a String:

    from collections import Counter
    def first_non_repeated_char(s):
        count = Counter(s)
        for char in s:
            if count[char] == 1:
                return char
  4. Anagram Check:

    def is_anagram(s1, s2):
        return sorted(s1) == sorted(s2)
  5. Find the Missing Number in a List:

The provided Python code defines a function named find_missing_number that takes a list of numbers (nums) as its input and returns the missing number in a sequence of consecutive numbers. The assumption is that the input list is missing exactly one number from a continuous sequence.

def find_missing_number(nums):
    n = len(nums) + 1
    expected_sum = n * (n + 1) // 2
    actual_sum = sum(nums)
    return expected_sum - actual_sum
  1. How to reverse the order of elements in a list without using a for loop?
# Ans:
original_list = [1, 2, 3, 4, 5]
reversed_list = list(reversed(original_list))
print(reversed_list)
  1. Two Sum:
    def two_sum(nums, target):
     seen = {}  # This dictionary will store the numbers we have seen so far along with their indices.
     for i, num in enumerate(nums):  # Iterate through the list of numbers along with their indices.
         complement = target - num  # Calculate the complement needed to reach the target.
         if complement in seen:
             # If the complement is in the 'seen' dictionary, we found a pair that adds up to the target.
             # Return the indices of the two numbers.
             return [seen[complement], i]
         seen[num] = i  # Store the current number in the 'seen' dictionary with its index.

The code you provided is an implementation of the Two Sum problem using a hash table for optimization. The Two Sum problem is a common coding interview question that asks to find two numbers in a given array (nums) that add up to a specific target value (target). The function two_sum takes the list of numbers and the target value as input and returns the indices of the two numbers that satisfy the condition.

 result = two_sum([2, 7, 11, 15], 9)
 print(result)

The output will be [0, 1], indicating that the numbers at indices 0 and 1 in the list add up to the target value. In this case, 2 + 7 = 9.

The algorithm uses a hash table (implemented with a dictionary in Python) to keep track of the numbers encountered so far and their indices. This allows for efficient lookups when checking for the complement needed to reach the target. The time complexity of this algorithm is O(n), where n is the length of the input list.

  1. Check for a Substring in a String:

    def is_substring(s, sub):
        return sub in s
  2. Maximum Subarray Sum:

    def max_subarray_sum(nums):
        max_sum = current_sum = nums[0]
        for num in nums[1:]:
            current_sum = max(num, current_sum + num)
            max_sum = max(max_sum, current_sum)
        return max_sum

The provided code defines a function called max_subarray_sum that calculates the maximum sum of a contiguous subarray (subarray with consecutive elements) within a given list of numbers (nums). The function uses Kadane's algorithm, a dynamic programming approach, to efficiently find the maximum subarray sum.

  nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
  result = max_subarray_sum(nums)
  print(result)

In this example, the input list nums is [-2, 1, -3, 4, -1, 2, 1, -5, 4]. The maximum subarray sum is 6, which corresponds to the subarray [4, -1, 2, 1]. The function will return 6 as the result.

  1. Merge Intervals:
    def merge_intervals(intervals):
        intervals.sort(key=lambda x: x[0])
        merged = []
        for interval in intervals:
            if not merged or merged[-1][1] < interval[0]:
                merged.append(interval)
            else:
                merged[-1][1] = max(merged[-1][1], interval[1])
        return merged

11: How to count the occurrences of each element in a list without using a for loop?

# Ans:
my_list = [1, 2, 2, 3, 4, 4, 5, 5, 5]
element_counts = {element: my_list.count(element) for element in set(my_list)}
print(element_counts)
  1. Implement a Queue:

    from collections import deque
    class Queue:
        def __init__(self):
            self.items = deque()
        def enqueue(self, item):
            self.items.append(item)
        def dequeue(self):
            return self.items.popleft()
        def is_empty(self):
            return len(self.items) == 0
  2. Calculate Fibonacci Sequence: Return the nth Fibonacci number

    def fibonacci(n):
        a, b = 0, 1
        for _ in range(n):
            a, b = b, a + b
        return a
     result = fibonacci(6)
     print(result)

In this example, fibonacci(6) returns the 6th Fibonacci number, which is 8. The sequence starts with 0, 1, 1, 2, 3, 5, 8, and so on. The function efficiently calculates Fibonacci numbers without using recursion by iteratively updating the values.

  1. Count Set Bits in an Integer:

    def count_set_bits(n):
        return bin(n).count('1')
  2. Find the Longest Increasing Subsequence:

    def length_of_lis(nums):
        if not nums:
            return 0
        dp = [1] * len(nums)
        for i in range(1, len(nums)):
            for j in range(i):
                if nums[i] > nums[j]:
                    dp[i] = max(dp[i], dp[j] + 1)
        return max(dp)
  3. Implement a Linked List:

    class Node:
        def __init__(self, data=None):
            self.data = data
            self.next = None
    
    class LinkedList:
        def __init__(self):
            self.head = None
  4. Check for a Cycle in a Linked List:

    def has_cycle(head):
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                return True
        return False
  5. Reverse a Linked List:

    def reverse_linked_list(head):
        prev = None
        current = head
        while current:
            next_node = current.next
            current.next = prev
            prev = current
            current = next_node
        return prev
  6. Find the Middle of a Linked List:

    def find_middle(head):
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow.data
  7. Detect a Loop in a Linked List:

    def detect_loop(head):
        seen = set()
        current = head
        while current:
            if current in seen:
                return True
            seen.add(current)
            current = current.next
        return False
  8. Convert Sorted Array to Binary Search Tree:

    class TreeNode:
        def __init__(self, val=0, left=None, right=None):
            self.val = val
            self.left = left
            self.right = right
    
    def sorted_array_to_bst(nums):
        if not nums:
            return None
        mid = len(nums) // 2
        root = TreeNode(nums[mid])
        root.left = sorted_array_to_bst(nums[:mid])
        root.right = sorted_array_to_bst(nums[mid+1:])
        return root
  9. Serialize and Deserialize a Binary Tree:

    class TreeNode:
        def __init__(self, val=0, left=None, right=None):
            self.val = val
            self.left = left
            self.right = right
    
    def serialize(root):
        if not root:
            return "null"
        left = serialize(root.left)
        right = serialize(root.right)
        return f"{{'val': {root.val}, 'left': {left}, 'right': {right}}}"
    
    def deserialize(data):
        if data == "null":
            return None
        data_dict = eval(data)
        root = TreeNode(data_dict

['val']) root.left = deserialize(data_dict['left']) root.right = deserialize(data_dict['right']) return root ```

  1. LCA (Lowest Common Ancestor) in a Binary Tree:

    class TreeNode:
        def __init__(self, val=0, left=None, right=None):
            self.val = val
            self.left = left
            self.right = right
    
    def lowest_common_ancestor(root, p, q):
        if not root or root == p or root == q:
            return root
        left = lowest_common_ancestor(root.left, p, q)
        right = lowest_common_ancestor(root.right, p, q)
        return root if left and right else left or right
  2. Topological Sort:

    from collections import defaultdict
    
    def topological_sort(graph):
        visited = set()
        stack = []
    
        def dfs(node):
            visited.add(node)
            for neighbor in graph[node]:
                if neighbor not in visited:
                    dfs(neighbor)
            stack.append(node)
    
        for node in graph:
            if node not in visited:
                dfs(node)
    
        return stack[::-1]
  3. Dijkstra's Algorithm:

    import heapq
    
    def dijkstra(graph, start):
        distances = {node: float('infinity') for node in graph}
        distances[start] = 0
        priority_queue = [(0, start)]
    
        while priority_queue:
            current_distance, current_node = heapq.heappop(priority_queue)
    
            if current_distance > distances[current_node]:
                continue
    
            for neighbor, weight in graph[current_node].items():
                distance = current_distance + weight
                if distance < distances[neighbor]:
                    distances[neighbor] = distance
                    heapq.heappush(priority_queue, (distance, neighbor))
    
        return distances
  4. Depth-First Search (DFS) on a Graph:

    def dfs(graph, node, visited):
        if node not in visited:
            visited.add(node)
            for neighbor in graph[node]:
                dfs(graph, neighbor, visited)
  5. Breadth-First Search (BFS) on a Graph:

    from collections import deque
    
    def bfs(graph, start):
        visited = set()
        queue = deque([start])
    
        while queue:
            node = queue.popleft()
            if node not in visited:
                visited.add(node)
                queue.extend(graph[node] - visited)
    
        return visited
  6. Heap Implementation:

    import heapq
    
    class MinHeap:
        def __init__(self):
            self.heap = []
    
        def push(self, item):
            heapq.heappush(self.heap, item)
    
        def pop(self):
            return heapq.heappop(self.heap)
    
        def top(self):
            return self.heap[0]
    
        def size(self):
            return len(self.heap)
  7. LRU Cache Implementation:

    from collections import OrderedDict
    
    class LRUCache:
        def __init__(self, capacity):
            self.cache = OrderedDict()
            self.capacity = capacity
    
        def get(self, key):
            if key not in self.cache:
                return -1
            else:
                value = self.cache.pop(key)
                self.cache[key] = value
                return value
    
        def put(self, key, value):
            if key in self.cache:
                self.cache.pop(key)
            elif len(self.cache) >= self.capacity:
                self.cache.popitem(last=False)
            self.cache[key] = value
  8. Regular Expression Matching:

    def is_match(s, p):
        if not p:
            return not s
        first_match = bool(s) and (s[0] == p[0] or p[0] == '.')
    
        if len(p) >= 2 and p[1] == '*':
            return (is_match(s, p[2:]) or
                    (first_match and is_match(s[1:], p)))
        else:
            return first_match and is_match(s[1:], p[1:])

31. Factorial:

Question: Write a function to calculate the factorial of a number.

Answer:

def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n-1)

32. Fibonacci Sequence:

Question: Write a function to generate the Fibonacci sequence up to n numbers.

Answer:

def fibonacci(n):
    fib_sequence = [0, 1]
    while len(fib_sequence) < n:
        fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])
    return fib_sequence

Remember, the key to successfully tackling coding interviews is not just knowing the correct answers but also being able to explain your thought process, optimize your solutions, and handle edge cases effectively.

Q33. Write Python code to print below pattern.

    * 
   * * 
  * * * 
 * * * * 
* * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    * 

Ans: Below is a simple Python code to print the given pattern:

def print_pattern(n):
    for i in range(1, n + 1):
        spaces = " " * (n - i)
        stars = "* " * i
        print(spaces + stars)

    for i in range(n - 1, 0, -1):
        spaces = " " * (n - i)
        stars = "* " * i
        print(spaces + stars)

# Set the desired number of rows
rows = 5

# Call the function to print the pattern
print_pattern(rows)

This code defines a function print_pattern that takes the number of rows (n) as input and prints the pattern accordingly. In this example, rows is set to 5, but you can change it to any other positive integer to adjust the size of the pattern.

The output for rows = 5 will be:

You can modify the rows variable to get a different-sized pattern.

Q 34: Code to write Fibonacci series?

Ans:

n = 5
a, b = 0, 1
fib_series = [a, b]
for _ in range(n):
    a, b = b, a + b
    fib_series.append(b)

print(fib_series)
Clone this wiki locally