Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/devAmoghS/Python-Interview-Problems-for-Practice
120+ Common code and interview problems solved in Python **(it's GROWING...)** Give a Star 🌟If it helps you. Please go through the README.md before starting.
https://github.com/devAmoghS/Python-Interview-Problems-for-Practice
algotithms data-structures data-structures-and-algorithms interview-prep interview-preparation interview-questions interviews programming-interviews python python-3
Last synced: 15 days ago
JSON representation
120+ Common code and interview problems solved in Python **(it's GROWING...)** Give a Star 🌟If it helps you. Please go through the README.md before starting.
- Host: GitHub
- URL: https://github.com/devAmoghS/Python-Interview-Problems-for-Practice
- Owner: devAmoghS
- License: mit
- Created: 2018-01-26T04:26:37.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-10-15T04:50:15.000Z (30 days ago)
- Last Synced: 2024-10-29T15:47:10.520Z (15 days ago)
- Topics: algotithms, data-structures, data-structures-and-algorithms, interview-prep, interview-preparation, interview-questions, interviews, programming-interviews, python, python-3
- Language: Jupyter Notebook
- Homepage:
- Size: 675 KB
- Stars: 977
- Watchers: 33
- Forks: 154
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_Of_CONDUCT.md
Awesome Lists containing this project
README
# Python-Interview-Problems-for-Practice (now supported with *Code Style*)
![GitHub stars](https://img.shields.io/github/stars/devAmoghS/Python-Interview-Problems-for-Practice?style=for-the-badge) ![GitHub forks](https://img.shields.io/github/forks/devAmoghS/Python-Interview-Problems-for-Practice?label=Forks&style=for-the-badge) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/python/black)## Star History
[![Star History Chart](https://api.star-history.com/svg?repos=devAmoghS/Python-Interview-Problems-for-Practice&type=Date)](https://star-history.com/#devAmoghS/Python-Interview-Problems-for-Practice&Date)
## Updates
| Problem | Link |
|---|---|
| Given an array of integers, find the pair of adjacent elements that has the largest product and return that product. | [adjacentElementProduct.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/adjacentElementProduct.py) |
| Convert the string "123" into 123, without using the built-api `int()` | [atoi.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/atoi.py) |
| Given an array , find if the number exists, develop a `recursive` implementation of the binary search. If the element is not found it returns -1 | [binary_search_recursive.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/binary_search_recursive.py) |
| Bresenham Line Algorithm (BLA) is one of the earliest algorithms developed in computer graphics. It is used for drawing lines | [bresenham_line_algorithm.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/bresenham_line_algorithm.py) |
| Find the no. of nodes in a BST that lies in a given range | [bst_nodes_in_range.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/bst_nodes_in_range.py) |
| Bubble Sort | [bubbleSort.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/bubble_sort.py) |
| Find the angle made by the hour hand and the minute hand at any given time. Assume it is an analog clock | [calculateClockAngle.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/calculateClockAngle.py) |
| Two strings of sizes m and n are given, we have to find how many characters need to be removed from both the string so that they become anagrams of each other | [check_anagrams.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/check_anagrams.py) |
| Find the numbers which are semiprimes, within a given range. A semiprime is a product of two prime numbers, not necessarily distinct. Squares of prime numbers are also semiprimes. | [check_semiprime.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/check_semiprime.py) |
| Given a graph, there are two methods to perform traversal on it 1. Depth First Search (DFS) 2. Breadth First Search (BFS) | [dfs_bfs.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/dfs_bfs.py) |
| The diameter of a tree (sometimes called the width) is the number of nodes on the longest path between two end nodes(leftmost leaf node and rightmost leaf node). | [diameterOfTree.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/diameterOfTree.py) |
| Estimate pi using random distribution | [estimate_pi.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/estimate_pi.py) |
| Write an efficient program for printing k largest elements in an array. Elements in array can be in any order. | [find_k_largest.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/find_k_largest.py) |
| Given a linked list, this method will return m'th element to the last 2->3->4->8->5; m=2 will return 8 since 8 is second to last | [find_m_to_last_llist.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/find_m_to_last_llist.py) |
| Given an array of numbers, find all the pairs of numbers which sum upto `k`| [find_pairs_sum_k.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/find_pairs_sum_k.py) |
| Given an array of numbers, find all the pairs of numbers whose product is `k` | [find_products_pair_k.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/find_products_pair_k.py) |
| Given an array of integers, write a function that returns true if there is a triplet (a, b, c) that satisfies a^2 + b^2 = c^2. | [find_pythagoras_triplet.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/find_pythagoras_triplet.py) |
| Given a binary tree, find the second largest node in it | [find_second_largest_in_binary_tree.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/find_second_largest_in_binary_tree.py) |
| Write a function that computes the list of the first 100 Fibonacci numbers | [first_n_fibo.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/first_n_fibo.py) |
| Given an input string, it gives the first non repeating character in it | [first_non_repeating.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/first_non_repeating.py) |
| Given an input string, find the first recurring character in it. | [first_recurring_character.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/first_recurring_character.py) |
| Given a string, find the first non-repeating character in it. For example, if the input string is “GeeksforGeeks”, then output should be ‘f’ and if input string is “GeeksQuiz”, then output should be ‘G’. | [first_unique_letter.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/first_unique_letter.py) |
| Write a function that given a list of non negative integers, arranges them such that they form the largest possible number. For example, given [50, 2, 1, 9], the largest formed number is 95021 | [gen_largest_num_frm_list.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/gen_largest_num_frm_list.py) |
| Tree Data Structure in Python | [general_tree_structure.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/general_tree_structure.py) |
| Given the arrival and departure times of all trains that reach a railway station, the task is to find the minimum number of platforms required for the railway station so that no train waits.
We are given two arrays that represent the arrival and departure times of trains that stop.| [getMinPlatforms.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/getMinPlatforms.py) |
| Print duplicate characters in a string | [get_dup_chars.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/get_dup_chars.py) |
| Check if a given array has zero sum sub array | [hasZeroSumSubArray.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/hasZeroSumSubArray.py) |
| Given a string, check if it only contains digits | [has_only_digits.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/has_only_digits.py) |
| Given two lat, long coordinates , calculate `Haversine` distance between them | [haversine.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/haversine.py) |
| Heap Data Structure | [heap_structure.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/heap_structure.py) |
| Print numbers 1 to 100 without using any numbers or integers | [hundred_without_int.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/hundred_without_int.py) |
| Write a program to convert interger to Roman Representation | [interger_to_roman_num.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/interger_to_roman_num.py) |
| Given two sorted array of sizes m and n in which all elements are distinct. Find the common elements between them | [intersection_arrays.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/intersection_arrays.py) |
| Check if a matrix is symmetric or not | [isMatrixSymmetric.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/isMatrixSymmetric.py) |
| Check if two strings are anagrams of each other | [is_anagram.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/is_anagram.py) |
| Check if two strings are anagrams of each other | [is_anagram_using_collections.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/is_anagram_using_collections.py) |
| Write a program to check if a number is a palindrome or not | [is_num_palindrome.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/is_num_palindrome.py) |
| Given a string, return True if it is a numeric data type, False otherwise | [is_numeric.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/is_numeric.py) |
| N soldiers are standing in a circle and first person has sword and he kills the 2nd person and gives the sword to the third person and so on till 99th person kills the 100th person gives the sword back to the first person, this goes on till only one person survives. Print the survivor. | [josephus.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/josephus.py) |
| The effective time complexity of this improved version is O(logN). For the problem statement, refer `josephus.py` | [josephus_improved.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/josephus_improved.py) |
| The effective time complexity of this improved version is O(1). For the problem statement, refer `josephus.py` | [josephus_improved_v3.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/josephus_improved_v3.py) |
| Implement the Karatsuba algorithm | [karatsuba.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/karatsuba.py) |
| Implement Level Order traversal for a binary tree | [level_order_tree.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/level_order_tree.py) |
| Linked List Data Structure | [linked_list_data_structure.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/linked_list_data_structure.py) |
| Detect if a linked list has a loop | [loop_in_linkedlist.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/loop_in_linkedlist.py) |
| Find the Lowest Common ancestor between two nodes in a binary search tree. Let T be a rooted tree. The lowest common ancestor between two nodes n1 and n2 is defined as the lowest node in T that has both n1 and n2 as descendants (where we allow a node to be a descendant of itself).
The LCA of n1 and n2 in T is the shared ancestor of n1 and n2 that is located farthest from the root. | [lowest_common_ancestor.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/lowest_common_ancestor.py) |
| A majority element in an array A[] of size n is an element that appears more than n/2 times. Find the majority element in the given array. | [majority_element.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/majority_element.py) |
| Find the max number in an array without using built-in functions | [max_in_array.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/max_in_array.py) |
| Given a list of positive and negative numbers, find the maximum subarray sum. Constraint: Solve it in O(n) | [maximum_subarray_sum.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/maximum_subarray_sum.py) |
| Implementation of Merge Sort | [merge_sort.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/merge_sort.py) |
| Find the minimum and maximum numbers in an array in a single loop | [min_max_array_oneLoop.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/min_max_array_oneLoop.py) |
| Given an array of integers we need to move all the zeroes to the end and maintain the order of rest of the elements. Needless to say it should be an in-place solution | [move_zeros_to_end.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/move_zeros_to_end.py) |
| Print the nodes of a binary tree which do not have a sibling | [no_sibling_tree.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/no_sibling_tree.py) |
| Let all odd numbers come before even numbers, and sort the odd numbers in ascending order and even numbers in descending order. For example, the string '1982376455' becomes '1355798642' | [oddAscEvenDesc.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/oddAscEvenDesc.py) |
| Compute and print the pascal traingle, given the no. of levels | [pascal_triangle.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/pascal_triangle.py) |
| using factorial, reduced the time complexity of program from O(2^N) to O(N) | [pascals_triangle_improved.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/pascals_triangle_improved.py) |
| Print the permutations of a string | [permutations.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/permutations.py) |
| Print the permutatations of a string (Simplified) | [permute_strings.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/permute_strings.py) |
| Preorder Traversal of a Binary Search Tree in Iterative fashion | [preorder_iterative_bst.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/preorder_iterative_bst.py) |
| Implementation of a priority Queue | [priority_queue_simple.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/priority_queue_simple.py) |
| Process the string "k:1 |k1:2|k2:3|k3:4" into a dictionary {k:1,k1:2,...} | [processStringToDict.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/processStringToDict.py) |
| Given an array arr[] of n integers, construct a Product Array prod[] (of same size) such that prod[i] is equal to the product of all the elements of arr[] except arr[i]. | [product_puzzle.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/product_puzzle.py) |
| Implementation of a Queue Data Structure | [queue_data_structure.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/queue_data_structure.py) |
| Implementation of Quick Sort | [quick_sort.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/quick_sort.py) |
| Write an efficient function that deletes characters from an ASCII string where any character existing in remove must be deleted from str. For example, given a str of "Battle of the Vowels: Hawaii vs. Grozny" and a remove of "aeiou", the function should transform str to “Bttl f th Vwls: Hw vs. Grzny”. | [remove_chars.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/remove_chars.py) |
| Remove duplicate characters from string | [remove_dup_chars.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/remove_dup_chars.py) |
| Remove duplicates using a dictionary | [remove_duplicates.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/remove_duplicates.py) |
| Remove duplicates using extra space | [remove_duplicates_v2.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/remove_duplicates_v2.py) |
| Reverse the string in place | [reverse_in_place.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/reverse_in_place.py) |
| Reverse the string using recursion | [reverse_str_recursive.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/reverse_str_recursive.py) |
| Given a sentence, reverse each word but don't reverse the sentence | [reverse_words.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/reverse_words.py) |
| Given a matrix, rotate it by 180 degree | [rotateMatrix180Deg.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/rotateMatrix180Deg.py) |
| Find Median from Data Stream of integers | [running_median_integers.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/running_median_integers.py) |
| Given a sorted array in which all elements appear twice (one after one) and one element appears only once. Find that element Constraints: in O(log n) complexity. | [search_unique.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/search_unique.py) |
| A simple implementation of Selection Sort | [selection_sort.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/selection_sort.py) |
| A simple implementation of Stack Data Structure | [stack_data_structure.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/stack_data_structure.py) |
| The span Si of the stock’s price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day.
Problem: We have a series of n daily price quotes for a stock and we need to calculate span of stock’s price for all n days | [stock_span.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/stock_span.py) |
| Write a program to sum a given array using recursion | [sum_array_recursion.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/sum_array_recursion.py) |
| You are getting two streams of data with dates as key, merge the two streams and return average of the values if there is a common date else just update the value as received in the stream, refer example in the code | [timeseries.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/timeseries.py) |
| Given two sorted array of sizes m and n in which all elements are distinct. Find the union between them Constraints: in O(m+n) complexity. | [union_arrays.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/union_arrays.py) |
| Username validation program | [username_validation.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/username_validation.py) |
| Given an array of integers(+ve,-ve and 0) find the sign of the product of all the given values. | [signOfProduct.py](https://github.com/devAmoghS/Python-Interview-Problems-for-Practice/blob/master/signOfProduct.py) |
| | []() |
| | []() |
| | []() |
| | []() |
| | []() |
| | []() |
| | []() |#### Problems available: Solved -- 63 ; Newly Added -- 57
#### 1. Code Style is added
#### 2. 57 new problems are added in `Interview_Questions.md`. Solutions to be uploaded soon.![alt text](https://images.unsplash.com/photo-1494178270175-e96de2971df9?ixlib=rb-1.2.1&auto=format&fit=crop&w=972&q=80)
40+ Common code and interview problems solved in Python (it's growing...)### The core idea is not to utilize built-in functions or library and giving it a more logic-based approach, so that it can be language-friendly and not end up being another repository of "Python Tips and Tricks" .
## How good is the code ?
* It is well tested
* Consistent formatting (using [Black](https://github.com/python/black))
* It can compile in its current state (and there are relatively no issues)## How much support is available?
* FAQs (coming soon)
* Documentation (coming soon)## Issues
Feel free to submit issues and enhancement requests.## Contributing
Please refer to each project's style guidelines and guidelines for submitting patches and additions. In general, we follow the "fork-and-pull" Git workflow.1. **Fork** the repo on GitHub
2. **Clone** the project to your own machine
3. **Commit** changes to your own branch
4. **Push** your work back up to your fork
5. Submit a **Pull request** so that we can review your changesNOTE: Be sure to merge the latest from "upstream" before making a pull request!
# LICENSE
MIT License