Python Decoded: 10 Essential Python Programs For Interview & Beyond

Trinesh
6 min readJul 21, 2023

--

Photo by Sebastian Herrmann on Unsplash

In this blog, we’ll explore 10 essential Python programs that often serve as common interview questions. These programs cover a wide range of topics, from basic data structures to string manipulation and recursion. By understanding and mastering these Python programs, you’ll be well-prepared to tackle coding challenges in interviews and showcase your problem-solving skills to potential employers. Let’s dive into these insightful examples to enhance your Python proficiency and interview readiness.

1. Given a string, we need to replace all commas with dots and all dots with the commas.

  • Using python inbuilt function replace() let’s solve this problem
  • Replace , to a variable third and . to the , again replace third variable with .

2. Given a string, find all the duplicate letters of the string

  • Iterate through string and store each elements in a dictionary
  • While iterating skip spaces and count only letters, if the letter is not in dictionary add it if already present in dictionary increment the count
  • Once you have the dictionary iterate through dictionary keys, and values. If the count is more than one append to empty list
  • Return list of duplicates

You can also solve the same program by using a python class Counter from collections module

In Python, Counter is a built-in class provided by the collections module. It is used to count the occurrences of elements in a collection (e.g., a list, tuple, or string). The Counter class is a specialized dictionary (subclass of the dict class) that helps simplify the process of counting items in a collection.

3. Given a string, the task is to find the frequencies of all the characters in that string and return a dictionary with key as the character and its value as its frequency in the given string

  • Create an empty dictionary to store keys and values of a string and their frequency of occurrence
  • Iterate through given string
  • As we know Python is a case sensitive programming language. variable and Variable are two different variables in python. So we have convert the input string to either-lower or upper case
  • In each iteration through string, check if the letters are present in dictionary if not present append to dictionary if already present increment the value count
  • Return the dictionary

4. Given two strings, check if the substring is present in the main string

In Python, the in keyword is used as a membership operator.

  • It is used to check whether a value exists within a sequence (like a string, list, tuple, set, or dictionary) or not.
  • The result of using the in operator is a Boolean value (True or False) indicating whether the value is present in the sequence or not.

This program can be solved in many different ways, one among them is using find method

  • In Python, the find() method is a built-in method for strings. It is used to search for the first occurrence of a substring within a string and returns the index of the found substring. If the substring is not found, it returns -1

5. Given a string S, reverse the string without reversing its individual words.

We’ll use split() method to solve this problem

  • Take an input as string and use split() method which will return in the form of list
  • Using reverse() method reverse the list
  • Using join method join the lists with space in between and return the string

Let’s try the same code to join with . The only this we have to change here is while joining insert a .

6. Write a program to check if the given number is prime number

  • Initialize flag to False and for the number greater than 1 iterate through for loop from 2 to the input num
  • Check if any of the i value is divides the input num and and if remainder is 0 break and assign True value to the flag variable
  • If none of i value divides num, then it should be a prime number

7. Write a program to check if the given number is palindrome or not

A palindrome is a word, phrase, number, or any other sequence of characters that reads the same forwards and backwards.

  • A variable temp is initialized with the value of num. This variable will be used to store a temporary copy of the original number during the process of reversing it.
  • A variable reverse is initialized with the value 0. This variable will be used to store the reversed version of the number.
  • In each iteration of the loop, the variable remainder is assigned the value of the last digit of the temp number by taking the modulus (%) with 10
  • The reverse variable is updated by appending the last digit of the temp number to its right. This effectively reverses the digits of the number
  • The last digit is removed from temp by using integer division (//) with 10. This is done to move on to the next digit in the next iteration

In other words, it remains unchanged when its characters are reversed. For example, some palindromic words are “level,” “radar,” “deified,” and “madam.”

8. Write a program to check if the given two string are anagram

sorted() is a built-in function that takes an iterable (e.g., a list, tuple, or string) as input and returns a new sorted list containing all the elements from the original iterable in ascending order. The original iterable remains unchanged

9. Write a program to find factorial of a given number

  • Initialize variable factorial to 1
  • If num is greater than use for loop to iterate over range from 1 to num and multiply each number with factorial value

10. Write a program to print different star patterns

This code prints a pattern of asterisks (*) in the form of right-angled triangles.

  • The outer loop runs n times, where n = 5, controlling the number of rows in the triangle.
  • The inner loop runs from 0 to i (inclusive) in each iteration of the outer loop, controlling the number of asterisks printed in each row.
  • The print('*', end=' ') statement prints the asterisks with a space after each, and print() is used to move to the next line, creating a new row in the triangle. The number of asterisks in each row increases with the row number, forming a right-angled triangle pattern.

This code prints a pattern of right-angled triangle using asterisks (*) using while loop. The number of rows in the triangle is determined by the value of n, which is set to 5. Each row contains i number of asterisks, where i increases from 1 to n in each row.

This code prints an inverted right-angled triangle of stars (*) using nested loops.

  • The outer loop iterates n times, and the inner loop iterates from the current value of i to n, printing a star (*) on the same line.
  • Each iteration of the outer loop prints a new line, resulting in the triangle shape.

This pattern is simply nesting for loops, two right angled triangles

In conclusion, this blog has explored 10 essential Python programs that frequently appear as common interview questions. By studying and mastering these programs, you have gained insights into various topics, ranging from basic data structures to string manipulation and recursion.

These programs equip you with valuable problem-solving skills and demonstrate your Python proficiency during interviews. From replacing characters in a string to printing intricate star patterns, you’ve tackled a diverse set of challenges that will undoubtedly boost your confidence in coding interviews.

Remember that practice is key to mastering these concepts, so keep experimenting with the code examples provided. By consistently honing your skills, you’ll be well-prepared to impress potential employers and excel in your future Python endeavors 🙏

--

--