This space is to list Python competitive programming questions.
You can use questions for coding practice. As we progress, I will keep adding more coding questions here.
This initiation is a part of our FREE Python online tutorial.
Table of Contents
This problem is asked in one of the HackerEarth contests.
Problem Statement: Little Robert likes mathematics. Today his teacher has given him two integers and asked him to find out how many integers can divide both numbers. Would you like to help him in completing his school assignment?
Input Formatting: There are two integers, a and b as input to the program.
Output Formatting: Print the number of common factors of a and b. Both the input values should be in a range of 1 to 10^12
.
Example:
Input: 10 15 Output: 2
Explanation: The common factors of 10 and 15 are 1 and 5. So the answer will be 2.
Python Code:
data = input() li = data.split() a = int(li[0]) b = int(li[1]) def gcd(a, b): if (a == 0): return b; return gcd(b%a, a); if (a>0 and a<(10**12+1) and b>=1 and b<(10**12+1)): count = 1 for i in range(2, gcd(a, b)+1): if a%i==0 and b%i==0: count = count+1 print(count)
Explanation:
split()
method.gcd()
is to find the greatest common divisor.gcd()
which uses the recursion technique. It is self-explanatory.This problem is asked in the HackerEarth contest.
Problem Statement: Consider a permutation of numbers from 1 to N written on a paper. Let’s denote the product of its element as ‘prod’ and the sum of its elements as ‘sum’. Given a positive integer N, your task is to determine whether ‘prod’ is divisible by ‘sum’ or not.
Input Format: The first input will be an integer T. It depicts a number of test cases. Followed by the value for each test case. Each test case will contain an integer N (1<= N <=10^9). It is nothing but the length of the permutation.
Output Format: For each test case, print “YEAH” if ‘prod’ is divisible by ‘sum’, otherwise print “NAH”.
Python Code:
testSize = int(input()) nArr=[] for i in range(1,testSize+1): nArr.append(int(input())) for n in nArr: if n>=1 and n<=(10**9): prod = 1 sum = 0 for i in range(1, n+1): prod = prod*i sum = sum+i if prod%sum==0: print("YEAH") else: print("NAH")
Input:
2 2 3
Output:
YEAH NAH
Explanation:
Python Code:
#write your code here def Solve (N,KΚ,Α): nResult=0 i=0 while i < N: nCount=0 bCheck=False while i < N and A[i] <= K: nCount = nCount + 1 if A[1] - K: bCheck=True i=i+1 if bCheck==True: nResult = nResult + nCount while i< N and A[i] > K: i=i+1 return nResult T = int(input()) for _ in range(T): N, K= map(int,input().split()) A=list(map(int,input().split())) out = Solve(N,K,A) print(out)
This question is asked in the Cohesity coding round on HackerEarth.
I hope that this code is self-explanatory.
This competitive coding question is asked in Goldman Sachs.
Problem Statement: Suppose you have given the stock prices for respective days like (100, 180, 260, 310, 40, 535, 695). The stock price for the 1st day is 100, the 2nd day it is 180, and so on. Write a Python program to determine what days the user should buy and sell the stocks to get the maximum profit.
In the above case, in the following scenarios user will get maximum profit.
Algorithm steps:
Python Program:
liStocks = [100, 180, 260, 310, 40, 535, 695] #find local minima def findMin(liStocks): for i, val in enumerate(liStocks[:-1]): if val < liStocks[i+1]: return i, val return -1, -1 #find local maxima def findMax(liStocks): for i, val in enumerate(liStocks[:-1]): if val > liStocks[i+1]: return i, val return i+1, liStocks[-1] def buySellStock(): index=0 while index < len(liStocks): i, val = findMin(liStocks[index:]) if i > -1: index=i+index print("bye stock on day ", index+1, val) else: break i, val = findMax(liStocks[index:]) index=i+index print("sell stock on day ", index+1, val) if __name__ == "__main__": buySellStock()
Output:
buy stock on day 1 100 sell stock on day 4 310 buy stock on day 5 40 sell stock on day 7 695
Note: The above program may not have covered all the corner test cases.
Here is the list of the competitive programming questions asked to the Python developer in the coding interview.
Thought…
I will keep adding more Python Competitive Programming Questions for Practice. Also, I explain each Python tutorial.
Follow this page to help yourself and to find some important tips for competitive programming.
Here is the code I have written for problem 0: Count Common Factor.
Thanks for sharing!
Please Upload Toughest Array Coding Questions Asked In Clavax Technologies.
Check these competitive coding questions asked in product-based companies.
My approach for question 1.
Interesting, Thanks for sharing!
How to save these questions?
Bookmark this page. We keep adding more questions to this list. You can always visit this page anytime.
Really helpful.
Thanks. I’m glad you find it helpful. Best wishes.