Thursday, June 6, 2024

Nitheen Kumar

Python Programs frequently asking in interviews

 

Certainly! Here are some Python programming problems that are commonly asked in interviews:

  1. Reverse a String: Write a Python function to reverse a given string.

    def reverse_string(s):
    return s[::-1]
  2. Check for Palindrome: Write a Python function to check if a given string is a palindrome.


    def is_palindrome(s): return s == s[::-1]
  3. Find the Largest Element in a List: Write a Python function to find the largest element in a given list.


    def find_largest_element(lst): return max(lst)
  4. Count the Occurrences of Each Word in a String: Write a Python function to count the occurrences of each word in a given string.

    def count_word_occurrences(s):
    words = s.split() word_count = {} for word in words: word_count[word] = word_count.get(word, 0) + 1 return word_count
  5. Remove Duplicates from a List: Write a Python function to remove duplicates from a given list.

    def remove_duplicates(lst):
    return list(set(lst))
  6. Check for Anagrams: Write a Python function to check if two given strings are anagrams of each other.

    def is_anagram(s1, s2):
    return sorted(s1) == sorted(s2)
  7. Find the Factorial of a Number: Write a Python function to find the factorial of a given number.

    def factorial(n):
    if n == 0: return 1 else: return n * factorial(n - 1)
  8. Check for Prime Number: Write a Python function to check if a given number is prime.

    def is_prime(n):
    if n <= 1: return False elif n <= 3: return True elif n % 2 == 0 or n % 3 == 0: return False i = 5 while i * i <= n: if n % i == 0 or n % (i + 2) == 0: return False i += 6 return True
  9. Find the Fibonacci Sequence: Write a Python function to generate the Fibonacci sequence up to a given number of terms.

    def fibonacci(n):
    fib_sequence = [0, 1] while len(fib_sequence) < n: fib_sequence.append(fib_sequence[-1] + fib_sequence[-2]) return fib_sequence
  10. Check for Armstrong Number: Write a Python function to check if a given number is an Armstrong number.

    def is_armstrong(n):
    order = len(str(n)) sum = 0 temp = n while temp > 0: digit = temp % 10 sum += digit ** order temp //= 10 return sum == n

These programming problems cover a range of common algorithms and techniques and can help assess a candidate's problem-solving skills and proficiency in Python.

  1. Check for Perfect Number: Write a Python function to check if a given number is a perfect number.

    def is_perfect_number(n):
    sum_of_divisors = sum([i for i in range(1, n) if n % i == 0]) return sum_of_divisors == n
  2. Find the Intersection of Two Lists: Write a Python function to find the intersection of two given lists.

    def find_intersection(list1, list2):
    return list(set(list1) & set(list2))
  3. Calculate the Power of a Number: Write a Python function to calculate the power of a given number.

    def power(base, exponent):
    return base ** exponent
  4. Check for Armstrong Number in a Range: Write a Python function to find and return all Armstrong numbers within a given range.

    def armstrong_numbers_in_range(start, end):
    armstrong_numbers = [] for num in range(start, end + 1): if is_armstrong(num): armstrong_numbers.append(num)
    Python Programs frequently asking in interviews

    return armstrong_numbers
  5. Calculate the GCD (Greatest Common Divisor) of Two Numbers: Write a Python function to calculate the GCD of two given numbers.

    def gcd(a, b):
    while b != 0: a, b = b, a % b return a
  6. Find the Sum of Natural Numbers: Write a Python function to find the sum of the first n natural numbers.

    def sum_of_natural_numbers(n):
    return n * (n + 1) // 2
  7. Check for Strong Number: Write a Python function to check if a given number is a strong number.

    def is_strong_number(n):
    factorial_sum = sum(factorial(int(digit)) for digit in str(n)) return factorial_sum == n
  8. Rotate a List: Write a Python function to rotate a given list to the left by a specified number of positions.

    def rotate_list_left(lst, k):
    k = k % len(lst) return lst[k:] + lst[:k]
  9. Calculate the LCM (Least Common Multiple) of Two Numbers: Write a Python function to calculate the LCM of two given numbers.

    def lcm(a, b):
    return (a * b) // gcd(a, b)
  10. Check for Happy Number: Write a Python function to check if a given number is a happy number.

    def is_happy_number(n):
    seen = set() while n != 1: n = sum(int(digit) ** 2 for digit in str(n)) if n in seen: return False seen.add(n) return True
  1. Find the Longest Common Subsequence (LCS) of Two Strings: Write a Python function to find the longest common subsequence of two given strings.

    def longest_common_subsequence(str1, str2):
    m, n = len(str1), len(str2) dp = [[0] * (n + 1) for _ in range(m + 1)] for i in range(1, m + 1): for j in range(1, n + 1): if str1[i - 1] == str2[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 else: dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) lcs = "" i, j = m, n while i > 0 and j > 0: if str1[i - 1] == str2[j - 1]: lcs = str1[i - 1] + lcs i -= 1 j -= 1 elif dp[i - 1][j] > dp[i][j - 1]: i -= 1 else: j -= 1 return lcs
  2. Implement the Sieve of Eratosthenes Algorithm to Find Prime Numbers: Write a Python function to generate all prime numbers up to a given limit using the Sieve of Eratosthenes algorithm.

    def sieve_of_eratosthenes(limit):
    primes = [] sieve = [True] * (limit + 1) for num in range(2, int(limit ** 0.5) + 1): if sieve[num]: primes.append(num) for multiple in range(num * num, limit + 1, num): sieve[multiple] = False for num in range(int(limit ** 0.5) + 1, limit + 1): if sieve[num]: primes.append(num) return primes
  3. Implement a Binary Search Algorithm: Write a Python function to perform a binary search on a sorted list and return the index of a given element, or -1 if not found.

    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
  4. Calculate the Edit Distance (Levenshtein Distance) Between Two Strings: Write a Python function to calculate the edit distance (Levenshtein distance) between two given strings.

    def edit_distance(str1, str2):
    m, n = len(str1), len(str2) dp = [[0] * (n + 1) for _ in range(m + 1)] for i in range(m + 1): for j in range(n + 1): if i == 0: dp[i][j] = j elif j == 0: dp[i][j] = i elif str1[i - 1] == str2[j - 1]: dp[i][j] = dp[i - 1][j - 1] else: dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) return dp[m][n]
  5. Implement a Depth-First Search (DFS) Algorithm: Write a Python function to perform a depth-first search traversal of a graph represented as an adjacency list.

    def dfs(graph, start, visited=None):
    if visited is None: visited = set() visited.add(start) print(start, end=' ') for neighbor in graph[start]: if neighbor not in visited: dfs(graph, neighbor, visited)
  6. Calculate the Maximum Subarray Sum: Write a Python function to find the contiguous subarray within a one-dimensional array with the largest sum.

    def max_subarray_sum(arr):
    max_sum = float('-inf') current_sum = 0 for num in arr: current_sum = max(num, current_sum + num) max_sum = max(max_sum, current_sum) return max_sum
  7. Implement a Topological Sorting Algorithm: Write a Python function to perform a topological sort on a directed acyclic graph (DAG) represented as an adjacency list.

    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]
  8. Find the Longest Increasing Subsequence (LIS) of a List: Write a Python function to find the length of the longest increasing subsequence of a given list of integers.

    def length_of_lis(nums):
    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)
  9. Implement a Breadth-First Search (BFS) Algorithm: Write a Python function to perform a breadth-first search traversal of a graph represented as an adjacency list.

    def bfs(graph, start):
    visited = set() queue = [start] while queue: node = queue.pop(0) if node not in visited: print(node, end=' ') visited.add(node) queue.extend(graph[node])
  10. Calculate the Longest Common Substring of Two Strings: Write a Python function to find the longest common substring of two given strings.

    def longest_common_substring(str1, str2):
    m, n = len(str1), len(str2) dp = [[0] * (n + 1) for _ in range(m + 1)] max_length = 0 end_index = 0 for i in range(1, m + 1): for j in range(1, n + 1): if str1[i - 1] == str2[j - 1]: dp[i][j] = dp[i - 1][j - 1] + 1 if dp[i][j] > max_length: max_length = dp[i][j] end_index = i else: dp[i][j] = 0 return str1[end_index - max_length: end_index]

These advanced-level Python programming problems cover topics such as dynamic programming, graph algorithms, string manipulation, and searching algorithms. They are designed to challenge candidates with strong programming skills and a deep understanding of algorithms and data structures.


Subscribe to get more Posts :