Hacker Rank - Python Questions
Hacker Rank - Python Questions
1. Problem
Given the participants’ score sheet for your University Sports Day, you are required to find the
runner-up score. You are given n scores. Store them in a list and find the score of the runner-up.
Input Format
The first line contains n. The second line contains an array A[] of n integers each separated by a
space.
Constraints
• 2 ≤ n ≤ 10
• -100 ≤ A[i] ≤ 100
Output Format
Print the runner-up score.
Sample Input
5
2 3 6 6 5
Sample Output
Solution:
if __name__ == '__main__':
n = int(input())
arr = map(int, input().split())
print(sorted(list(set(arr)))[-2])
1
Proprietary content. © 2013 - 2021 Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
2. Problem
The provided code stub reads and integer, n, from STDIN. For all non-negative integers i < n,
print i2 .
Example
n=3
The list of non-negative integers that are less than n = 3 is [ 0, 1, 2 ]. Print the square of each
number on a separate line.
0
1
4
Input Format
The first and the only line contains the integer, n.
Constraints
1 ≤ n ≤ 20
Output Format
Print n lines, one corresponding to each i.
Sample Input
Sample Output
0
1
4
9
16
Solution:
2
Proprietary content. © 2013 - 2021 Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
if __name__ == '__main__':
n = int(input())
for i in range(0, n):
print(i ** 2)
3. Problem
An extra day is added to the calendar almost every four years as February 29, and the day is
called a leap day. It corrects the calendar for the fact that our planet takes approximately 365.25
days to orbit the sun. A leap year contains a leap day.
In the Gregorian calendar, three conditions are used to identify leap years:
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800,
1900, 2100, 2200, 2300 and 2500 are NOT leap years.
Task
Given a year, determine whether it is a leap year. If it is a leap year, return the Boolean True,
otherwise return False.
Note that the code stub provided reads from STDIN and passes arguments to the is_leap
function. It is only necessary to complete the is_leap function.
Input Format
Read year, the year to test.
Constraints
1900 <= year <= 10^5
Output Format
The function must return a Boolean value (True/False). Output is handled by the provided code
stub.
3
Proprietary content. © 2013 - 2021 Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Sample Input
1990
Sample Output
False
Solution:
def is_leap(year):
leap = False
return leap
year = int(input())
print(is_leap(year))
4. Problem
Let’s learn about list comprehensions! You are given three integers x, y and z representing the
dimensions of a cuboid along with an integer n. Print a list of all possible coordinates given by
(i, j, k) on a 3D grid where the sum of i + j + k is not equal to n. Here, 0 <= i <= x; 0 <= j <= y; 0
<= k <= z. Please use list comprehensions rather than multiple loops, as a learning exercise.
Example
x=1
y=1
z=2
n=3
4
Proprietary content. © 2013 - 2021 Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
Print an array of the elements that do not sum to n = 3.
[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 2]]
Input Format
Four integers x, y, z and n, each on a separate line.
Constraints
Print the list in lexicographic increasing order.
Sample Input
1
1
1
2
Sample Output
[[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]
Solution:
if __name__ == '__main__':
x = int(input())
y = int(input())
z = int(input())
n = int(input())
output = [];
abc = [];
for X in range(x+1):
for Y in range(y+1):
for Z in range(z+1):
if X+Y+Z != n:
abc = [X,Y,Z];
output.append(abc);
print(output);
5
Proprietary content. © 2013 - 2021 Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
5. Problem
In this challenge, the user enters a string and a substring. You have to print the number of times
that the substring occurs in the given string. String traversal will take place from left to right, not
from right to left.
Input Format
The first line of input contains the original string. The next line contains the substring.
Constraints
1 <= len(string) <= 200
Each character in the string is an ascii character.
Output Format
Output the integer number indicating the total number of occurrences of the substring in the
original string.
Sample Input
ABCDCDC
CDC
Sample Output
Solution:
6
Proprietary content. © 2013 - 2021 Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.
if __name__ == '__main__':
string = input().strip()
sub_string = input().strip()
7
Proprietary content. © 2013 - 2021 Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited.