Python Coding Questions 1-100 (1)
Python Coding Questions 1-100 (1)
S.No Description
1. Given a string s, return the longest
Palindromic substring in s.
Example 1:
Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.
Example 2:
Input: s = "cbbd"
Output: "bb"
Constraints:
1 <= s.length <= 1000
s consist of only digits and English letters.
2. Given an array of integers nums and an integer target, return indices of the
two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you
may not use the same element twice.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
Constraints:
2 <= nums.length <= 10^4
-10^9 <= nums[i] <= 10^9
-10^9 <= target <= 10^9
Only one valid answer exists.
3. You are given the heads of two sorted linked lists list1 and list2.
Merge the two lists into one sorted list. The list should be made by splicing
together the nodes of the first two lists.
Example 1:
Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]
Example 2:
Input: list1 = [], list2 = []
Output: []
Example 3:
Input: list1 = [], list2 = [0]
Output: [0]
Constraints:
The number of nodes in both lists is in the range [0, 50].
-100 <= Node.val <= 100
Both list1 and list2 are sorted in non-decreasing order.
Change the array nums such that the first k elements of nums contain the
unique elements in the order they were present in nums initially. The
remaining elements of nums are not important as well as the size of nums.
Return k.
Example 1:
Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]
Explanation: Your function should return k = 2, with the first two elements of
nums being 1 and 2 respectively.
It does not matter what you leave beyond the returned k (hence they are
underscores).
Example 2:
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of
nums being 0, 1, 2, 3, and 4 respectively.
It does not matter what you leave beyond the returned k (hence they are
underscores).
Constraints:
1 <= nums.length <= 3 * 10^4
-100 <= nums[i] <= 100
nums is sorted in non-decreasing order.
Roman numerals are usually written largest to smallest from left to right.
However, the numeral for four is not IIII. Instead, the number four is written
as IV. Because the one is before the five we subtract it making four. The
same principle applies to the number nine, which is written as IX. There are
six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.
Example 1:
Input: s = "III"
Output: 3
Explanation: III = 3.
Example 2:
Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
Example 3:
Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
1 <= s.length <= 15
s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
It is guaranteed that s is a valid roman numeral in the range [1, 3999].
Example 1:
Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.
Example 2:
Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes
121-. Therefore it is not a palindrome.
Example 3:
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Constraints:
-2^31 <= x <= 2^31 - 1
Example 1:
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.
Example 2:
Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is "moon" with length 4.
Example 3:
Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.
Constraints:
1 <= s.length <= 10^4
s consists of only English letters and spaces ' '.
There will be at least one word in s.
Example 1:
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
Example 3:
Input: s = " "
Output: true
Explanation: s is an empty string "" after removing non-alphanumeric
characters.
Since an empty string reads the same forward and backward, it is a
palindrome.
Constraints:
1 <= s.length <= 2 * 10^5
s consists only of printable ASCII characters.
Constraints:
1 <= numRows <= 30
Example 1:
Input: rowIndex = 3
Output: [1,3,3,1]
Example 2:
Input: rowIndex = 0
Output: [1]
Example 3:
Input: rowIndex = 1
Output: [1,1]
Constraints:
0 <= rowIndex <= 33
Example 1:
Input: nums = [2,2,1]
Output: 1
Example 2:
Input: nums = [4,1,2,1,2]
Output: 4
Example 3:
Input: nums = [1]
Output: 1
Constraints:
1 <= nums.length <= 3 * 10^4
-3 * 10^4 <= nums[i] <= 3 * 10^4
Each element in the array appears twice except for one element which
appears only once.
12. Given two strings needle and haystack, return the index of the first
occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "sadbutsad", needle = "sad"
Output: 0
Explanation: "sad" occurs at index 0 and 6.
The first occurrence is at index 0, so we return 0.
Example 2:
Input: haystack = "PythonCode", needle = "Pythoc"
Output: -1
Explanation: "Pythoc" did not occur in "PythonCode", so we return -1.
Constraints:
1 <= haystack.length, needle.length <= 10^4
haystack and needle consist of only lowercase English characters.
13. Given a sorted array of distinct integers and a target value, return the index
if the target is found. If not, return the index where it would be if it were
inserted in order.
You must write an algorithm with O(log n) runtime complexity.
Example 1:
Input: nums = [1,3,5,6], target = 5
Output: 2
Example 2:
Input: nums = [1,3,5,6], target = 2
Output: 1
Example 3:
Input: nums = [1,3,5,6], target = 7
Output: 4
Constraints:
1 <= nums.length <= 10^4
-10^4 <= nums[i] <= 10^4
nums contains distinct values sorted in ascending order.
-10^4 <= target <= 10^4
14. Write a function to find the longest common prefix string amongst an array
of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: strs = ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.
Constraints:
1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] consists of only lowercase English letters.
Example 1:
Input: n = 1
Output: true
Explanation: 2^0 = 1
Example 2:
Input: n = 16
Output: true
Explanation: 2^4 = 16
Example 3:
Input: n = 3
Output: false
Constraints:
-2^31 <= n <= 2^31 - 1
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Constraints:
1 <= s.length, t.length <= 5 * 10^4
s and t consist of lowercase English letters.
17. Given an integer array nums, return the third distinct maximum number in
this array. If the third maximum does not exist, return
the maximum number.
Example 1:
Input: nums = [3,2,1]
Output: 1
Explanation:
The first distinct maximum is 3.
The second distinct maximum is 2.
The third distinct maximum is 1.
Example 2:
Input: nums = [1,2]
Output: 2
Explanation:
The first distinct maximum is 2.
The second distinct maximum is 1.
The third distinct maximum does not exist, so the maximum (2) is returned
instead.
Example 3:
Input: nums = [2,2,3,1]
Output: 1
Explanation:
The first distinct maximum is 3.
The second distinct maximum is 2 (both 2's are counted together since they
have the same value).
The third distinct maximum is 1.
Constraints:
1 <= nums.length <= 10^4
-2^31 <= nums[i] <= 2^31 - 1
18. Given two non-negative integers, num1 and num2 represented as string,
return the sum of num1 and num2 as a string.
You must solve the problem without using any built-in library for handling
large integers (such as BigInteger). You must also not convert the inputs to
integers directly.
Example 1:
Input: num1 = "11", num2 = "123"
Output: "134"
Example 2:
Input: num1 = "456", num2 = "77"
Output: "533"
Example 3:
Input: num1 = "0", num2 = "0"
Output: "0"
Constraints:
1 <= num1.length, num2.length <= 10^4
num1 and num2 consist of only digits.
num1 and num2 don't have any leading zeros except for the zero itself.
19. Given a string s containing just the characters '(', ')', '{', '}', '[' and ']',
determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Every close bracket has a corresponding open bracket of the same type.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Constraints:
1 <= s.length <= 10^4
s consists of parentheses only '()[]{}'.
Example 1:
Input: s = "abab"
Output: true
Explanation: It is the substring "ab" twice.
Example 2:
Input: s = "aba"
Output: false
Example 3:
Input: s = "abcabcabcabc"
Output: true
Explanation: It is the substring "abc" four times or the substring "abcabc"
twice.
Constraints:
1 <= s.length <= 10^4
s consists of lowercase English letters.
21. Given a list of numbers, you have to sort them in non decreasing order.
Input Format
The first line contains a single integer N, denoting the number of integers in
the list.
The next N lines contain a single integer each, denoting the elements of the
list.
Output Format
Output N lines, containing one integer each, in non-decreasing order.
Constraints
1≤N≤10^6
0≤0≤ elements of the list ≤10^6≤10^6
Input :5 5 3 6 7 1
Output :1 3 5 6 7
22. Recently, Ravi visited his doctor. The doctor advised Chef to drink at
least 2000 ml of water each day.
Ravi drank X ml of water today. Determine if Ravi followed the doctor's
advice or not.
Input Format
The first line contains a single integer T — the number of test cases. Then
the test cases follow.
The first and only line of each test case contains one integer X — the amount
of water Ravi drank today.
Output Format
For each test case, output YES if Ravi followed the doctor's advice of drinking
at least 2000 ml of water. Otherwise, output NO.
You may print each character of the string in uppercase or lowercase (for
example, the strings YES, yEs, yes, and yeS will all be treated as identical).
Constraints
1≤T≤2000
1≤X≤4000
Input : Output:
3 YES
2999 NO
1450 YES
2000
23. Sita and Geetha are playing with dice. In one turn, both of them roll their
dice at once.
They consider a turn to be good if the sum of the numbers on their dice is
greater than 6.
Given that in a particular turn Sita and Geetha got X and Y on their
respective dice, find whether the turn was good.
Input Format
The first line of input will contain a single integer T, denoting the number of
test cases.
Each test case contains two space-separated integers X and Y — the
numbers Sita and Geetha got on their respective dice.
Output Format
For each test case, output on a new line, YES, if the turn was good
and NO otherwise.
Each character of the output may be printed in either uppercase or
lowercase. That is, the strings NO, no, nO, and No will be treated as
equivalent.
Constraints
1≤T≤100
1≤X,Y≤6
Input : 4 Output:
14 NO
34 YES
42 NO
26 YES
24. Harsh was recently gifted a book consisting of N pages. Each page contains
exactly M words printed on it. As he was bored, he decided to count the
number of words in the book.
Help Harsh find the total number of words in the book.
Input Format
The first line of input will contain a single integer T, denoting the number of
test cases.
Each test case consists of two space-separated integers on a single
line, N and M — the number of pages and the number of words on each
page, respectively.
Output Format
For each test case, output on a new line, the total number of words in the
book.
Constraints
1≤T≤100
1≤N≤100
1≤M≤100
Input :4 Output:
11 1
42 8
24 8
95 42 3990
25. Chef is fond of burgers and decided to make as many burgers as possible.
Chef has A patties and B buns. To make 1 burger, Chef needs 1 patty
and 1 bun.
Find the maximum number of burgers that Chef can make.
Input Format
The first line of input will contain an integer T — the number of test cases.
The description of T test cases follows.
The first and only line of each test case contains two space-separated
integers A and B, the number of patties and buns respectively.
Output Format
For each test case, output the maximum number of burgers that Chef can
make.
Constraints
1≤T≤1000
1≤A,B≤105
Input: 4 Output:
22 2
23 2
32 2
23 17 17
26. Sunil aims to be the richest person in Iceland by his new restaurant
franchise. Currently, his assets are worth A billion dollars and have no
liabilities. He aims to increase his assets by X billion dollars per year.
Also, all the richest people in Iceland are not planning to grow and maintain
their current worth.
To be the richest person in Iceland, he needs to be worth at least B billion
dollars. How many years will it take Sunil to reach his goal if his value
increases by X billion dollars each year?
Input
The first line contains an integer T, the number of test cases. Then the test
cases follow.
Each test case contains a single line of input, three integers A, B, X.
Output
For each test case, output in a single line the answer to the problem.
Constraints
1≤T≤21 000
100≤A<B≤200
1≤X≤50
X divides B−A
Input : 3 Output:
100 200 10 10
111 199 11 8
190 200 10 1
27. Chef will have N guests in his house today. He wants to serve at least one
dish to each of the N guests. Chef can make two types of dishes. He needs
one fruit and one vegetable to make the first type of dish and one vegetable
and one fish to make the second type of dish. Now Chef
has A fruits, B vegetables, and C fishes in his house. Can he prepare at
least N dishes in total?
Input Format
First line will contain T, number of testcases. Then the testcases follow.
Each testcase contains of a single line of input, four integers N,A,B,C.
Output Format
For each test case, print "YES" if Chef can prepare at least N dishes,
otherwise print "NO". Print the output without quotes.
Constraints
1≤T≤100
1≤N,A,B,C≤100
Input : 4 Output:
2121 YES
3222 NO
4263 YES
3131 NO
28. You are given two integers N and K. You may perform the following
operation any number of times (including zero): change N to N−K, i.e.
subtract K from N. Find the smallest non-negative integer value of N you can
obtain this way.
Input
The first line of the input contains a single integer T denoting the number of
test cases. The description of T test cases follows.
The first and only line of each test case contains two space-separated
integers N and K.
Output
For each test case, print a single line containing one integer — the smallest
value you can get.
Constraints
1≤T≤10^5
1≤N≤10^9
0≤K≤10^9
Input : 3 Output:
52 1
44 0
25 2
29. Alice and Bob are playing a game of Blobby Volley. In this game, in each
turn, one player is the server and the other player is the receiver. Initially,
Alice is the server, and Bob is the receiver.
If the server wins the point in this turn, their score increases by 1, and they
remain as the server for the next turn.
But if the receiver wins the point in this turn, their score does not increase.
But they become the server in the next turn.
In other words, your score increases only when you win a point when you
are the server.
Please see the Sample Inputs and Explanation for more detailed explanation.
They start with a score of 00 each, and play N turns. The winner of each of
those hands is given to you as a string consisting of 'A's and 'B's. 'A' denoting
that Alice won that point, and 'B' denoting that Bob won that point. Your job
is the find the score of both of them after the N turns.
Input Format
The first line of input will contain a single integer T, denoting the number of
test cases.
Each test case consists of two lines of input.
The first line of each test case contains one integer N — the number of
turns.
The line contains a string S of length N.
If the ith character of this string is 'A', then Alice won that point.
If the ith character of this string is 'B', then Bob won that point.
Output Format
For each test case, output on a new line, two space-separated integers -
Alice's final score, and Bob's final score.
Constraints
1≤T≤1000
1≤N≤1000
Length of ∣S∣ = N
S consists only of the characters 'A' and 'B'.
Input: 4 Output:
3 30
AAA 03
4 11
BBBB 00
5
ABABB
5
BABAB
30. John has a string S with him. John is happy if the string contains a contiguous
substring of length strictly greater than 22 in which all its characters are
vowels.
Input: 4 Output:
Aeiou Happy
Abxy Sad
Aebcdefghij Sad
Abcdeeafg Happy
31. You are given a positive integer . Print a numerical triangle of height like the
one below:
1
22
333
4444
55555
......
Can you do it using only arithmetic operations, a single for loop and print
statement?
Use no more than two lines. The first line (the for statement) is already
written for you. You have to complete the print statement.
Note: Using anything related to strings will give a score of .
Input Format
A single line containing integer, .
Constraints
1<=N<=9
Output Format
Print lines as explained above.
Sample Input
5
Sample Output
1
22
333
4444
33. The provided code stub will read in a dictionary containing key/value pairs of
name:[marks] for a list of students. Print the average of the marks array for
the student name provided, showing 2 places after the decimal.
Input Format
The first line contains the integer , N the number of students' records. The
next lines contain the names and marks obtained by a student, each value
separated by a space. The final line contains query_name, the name of a
student to query.
Constraints
2<=n<=10
0<=marks[I]<=100
Length of marks arrays=3
Output Format
Print one line: The average of the marks obtained by the particular student
correct to 2 decimal places.
Sample Input 0
3
Krishna 67 68 69
Arjun 70 98 63
Malika 52 56 60
Malika
Sample Output 0
56.00
Sample Input 1
2
Harsh 25 26.5 28
Anurag 26 28 30
Harsh
Sample Output 1
26.50
34. There is an array of integers. There are also 2 disjoint sets, A and , B each
containing integers. You like all the integers in set A and dislike all the
integers in set B . Your initial happiness is 0. For each integer in the array, if I
belongs to A, you add i to your happiness. If ,I belongs to B you add -1 to
your happiness. Otherwise, your happiness does not change. Output your
final happiness at the end.
Note: Since A and B are sets, they have no repeated elements. However, the
array might contain duplicate elements.
Constraints
1<=n<=10^5
1<=m<=10^5
1<=Any integer in the input<=10^9
Input Format
The first line contains integers n and m separated by a space.
The second line contains n integers, the elements of the array.
The third and fourth lines contain m integers, A and B, respectively.
Output Format
Output a single integer, your total happiness.
Sample Input
32
153
31
57
Sample Output
1
35. You are given a string and your task is to swap cases. In other words, convert
all lowercase letters to uppercase letters and vice versa.
Function Description
Complete the swap_case function in the editor below.
swap_case has the following parameters:
string s: the string to modify
Returns
string: the modified string
Input Format
A single line containing a string s.
Constraints
0<len(s)<=1000
Sample Input 0
NriiT PresEnts CodinG ClaSses
Sample Output 0
nRIIt pRESeNTS cODINg cLAsSES
36. Consider the following:
A string,s , of length n where s=C0C1….Cn-1.
An integer,k , where k is a factor of n.
We can split s into n/k substrings where each subtring, ti , consists of a
contiguous block of characters in s. Then, use each ti to create
string ui such that:
The characters in ui are a subsequence of the characters in ti.
Any repeat occurrence of a character is removed from the string such that
each character in ui occurs exactly once. In other words, if the character at
some index j in ti occurs at a previous index < j in ti, then do not include the
character in string ui .
Given s and k , print n/k lines where each line denotes string .
Example
s=’AAABCADDE’
K=3
There are three substrings of length 3 to consider: 'AAA', 'BCA' and 'DDE'.
The first substring is all 'A' characters, so u1=’A’. The second substring has all
distinct characters, so u2=’BCA’ . The third substring has different
characters, so u3=’DE’ . Note that a subsequence maintains the original
order of characters encountered. The order of characters in each
subsequence shown is important.
Function Description
Complete the merge_the_tools function in the editor below.
merge_the_tools has the following parameters:
string s: the string to analyze
int k: the size of substrings to analyze
Prints
Print each subsequence on a new line. There will be n/k of them. No return
value is expected.
Input Format
The first line contains a single string, s.
The second line contains an integer,k , the length of each substring.
Constraints
1<=n<=10^4, where n is the length of s
1<=k<=n
It is guaranteed that n is a multiple of k .
Sample Input
STDIN Function
----- --------
AABCAAADA s = 'AABCAAADA'
3 k=3
Sample Output
AB
CA
AD
Constraints
0 < len(S) < 1000
0 < w < len(S)
Output Format
Print the text wrapped paragraph.
Sample Input 0
ABCDEFGHIJKLIMNOQRSTUVWXYZ
4
Sample Output 0
ABCD
EFGH
IJKL
IMNO
QRST
UVWX
YZ
38. You are given a complex z. Your task is to convert it to polar coordinates.
Input Format
A single line containing the complex number z. Note: complex() function can
be used in python to convert the input as a complex number.
Constraints
Given number is a valid complex number.
Output Format
Output two lines:
The first line should contain the value of r.
The second line should contain the value of q.
Sample Input
1+2j
Sample Output
2.23606797749979
1.1071487177940904
39. Mr. Vincent works in a door mat manufacturing company. One day, he
designed a new door mat with the following specifications:
Mat size must be N X M. (N is an odd natural number, and M is 3 times N.)
The design should have ‘WELCOME’ written in the center.
The design pattern should only use |, . and – characters.
Sample Designs
Size: 7 x 21
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
Size: 11 x 33
---------------.|.---------------
------------.|..|..|.------------
---------.|..|..|..|..|.---------
------.|..|..|..|..|..|..|.------
---.|..|..|..|..|..|..|..|..|.---
-------------WELCOME-------------
---.|..|..|..|..|..|..|..|..|.---
------.|..|..|..|..|..|..|.------
---------.|..|..|..|..|.---------
------------.|..|..|.------------
---------------.|.---------------
Input Format
A single line containing the space separated values of N and M.
Constraints
5 < N < 101
15 < M < 303
Output Format
Output the design pattern.
Sample Input
9 27
Sample Output
------------ . | . ------------
--------- . | . . | . . | . ---------
------ . | . . | . . | . . | . . |. ------
--- . | . . | . . | . . | . . | . . | . . | . ---
----------WELCOME----------
--- . | . . | . . | . . | . . | . . | . . | . ---
------ . | . . | . . | . . | . . | . ------
--------- . | . . |. . | . ---------
------------ . | . ------------
40. You are given a two lists A and B. Your task is to compute their cartesian
product A x B.
Example
A = [1, 2]
B = [3, 4]
41. Raghu is a shoe shop owner. His shop has X number of shoes.
He has a list containing the size of each shoe he has in his shop.
There are N number of customers who are willing to pay xi amount of
money only if they get the shoe of their desired size.
Your task is to compute how much money Raghu earned.
Input Format
The first line contains X, the number of shoes.
The second line contains the space separated list of all the shoe sizes in the
shop.
The third line contains N, the number of customers.
The next N lines contain the space separated values of the shoe size desired
by the customer and xi, the price of the shoe.
Constraints
0 < X < 10^3
0 < N <= 10^3
20 < xi < 100
2 < shoe size < 20
Output Format
Print the amount of money earned by Raghu.
Sample Input
10
2 3 4 5 6 8 7 6 5 18
6
6 55
6 45
6 55
4 40
18 60
10 50
Sample Output
200
42. There is a horizontal row of n cubes. The length of each cube is given. You
need to create a new vertical pile of cubes. The new pile should follow these
directions: if cube[i] is on top of cube[j] then sideLength|j| => sideLength|i|.
When stacking the cubes, you can only pick up either the leftmost or the
rightmost cube each time. Print Yes if it is possible to stack the cubes.
Otherwise, print No.
Example
blocks = [1, 2, 3, 8, 7]
Result: No
Choose blocks from right to left in order to successfully stack the blocks.
Input Format
The first line contains a single integer T, the number of test cases.
For each test case, there are 2 lines.
The first line of each test case contains n, the number of cubes.
The second line contains n space separated integers, denoting
the sideLengths of each cube in that order.
Constraints
1 <= T <= 5
1 <= n <= 10^5
1 <= sideLength < 2^31
Output Format
For each test case, output a single line containing either Yes or No.
Sample Input
STDIN Function
----- --------
2 T=2
6 blocks[] size n = 6
432134 blocks = [4, 3, 2, 1, 3, 4]
3 blocks[] size n = 3
132 blocks = [1, 3, 2]
Sample Output
Yes
No
43. You are given a function f(X) = X2. You are also given K lists. The ith list
consists of Ni elements.
You have to pick one element from each list so that the value from the
equation below is maximized:
S = (f(X1) + f(X2) + . . . + f(Xk) % M
Xi denotes the element picked from the ith list . Find the maximized
value Smax obtained. % denotes the modulo operator.
Note that you need to take exactly one element from each list, not
necessarily the largest element. You add the squares of the chosen elements
and perform the modulo operation. The maximum value that you can
obtain, will be the answer to the problem.
Input Format
The first line contains 2 space separated integers K and M.
The next K lines each contains an integer Ni, denoting the number of
elements in the ith list, followed by Ni space separated integers denoting the
elements in the list.
Constraints
1 <= K <= 7
1 <= M <= 1000
1 <= Ni <= 7
1 <= Magnitude of elements in list <= 109
Output Format
Output a single integer denoting the value Smax.
Sample Input
3 1000
254
3789
5 5 7 8 9 10
Sample Output
206
44. Mr. Anant Asankhya is the manager at the INFINITE hotel. The hotel has an
infinite amount of rooms.
One fine day, a finite number of tourists come to stay at the hotel.
The tourists consist of:
→ A Captain.
→ An unknown group of families consisting of K members per group
where K ≠ 1.
The Captain was given a separate room, and the rest were given one room
per group.
Mr. Anant has an unordered list of randomly arranged room entries. The list
consists of the room numbers for all of the tourists. The room numbers will
appear K times per group except for the Captain’s room.
Mr. Anant needs you to help him find the Captain’s room number.
The total number of tourists or the total number of groups of families is not
known to you.
You only know the value of K and the room number list.
Input Format
The first line consists of an integer, K, the size of each group.
The second line contains the unordered elements of the room number list.
Constraints
1 < K < 1000
Output Format
Output the Captain’s room number.
Sample Input
5
1236544253616532412514368431562
Sample Output
8
A strict superset has at least one element that does not exist in its subset.
Example
Set ([1 , 3, 4]) is a strict superset of set ([1 , 3]).
Set ([1 , 3, 4]) is not a strict superset of set ([1 , 3, 4]).
Set ([1 , 3, 4]) is not a strict superset of set ([1 , 3, 5]).
Input Format
The first line contains the space separated elements of set A.
The second line contains integer n, the number of other sets.
The next n lines contains the space separated elements of the other sets.
Constraints
0 < len(set(A)) < 50^1
0 < N < 2^1
0 < len(otherSets) < 10^1
Output Format
Print True if set A is a strict superset of all other N sets. Otherwise,
print False.
Sample Input 0
1 2 3 4 5 6 7 8 9 10 11 12 23 45 84 78
2
12345
100 11 12
Sample Output 0
False
46. You are given a spreadsheet that contains a list of N athletes and their
details (such as age, height, weight and so on). You are required to sort the
data based on the Kth attribute and print the final resulting table. Follow the
example given below for better understanding.
Note that K is indexed from 0 to M – 1, where M is the number of attributes.
Note: If two attributes are the same for different rows, for example, if two
atheletes are of the same age, print the row that appeared first in the input.
Input Format
The first line contains N and M separated by a space.
The next N lines each contain M elements.
The last line contains K.
Constraints
1 <= N, M <= 1000
0 <= K < M
Each element <= 1000
Output Format
Print the N lines of the sorted table. Each line should contain the space
separated elements. Check the sample below for clarity.
Sample Input 0
53
10 2 5
710
999
1 23 12
659
1
Sample Output 0
710
10 2 5
659
999
1 23 12
47. You are given a string N.
Your task is to verify that N is a floating point number.
In this task, a valid float number must satisfy all of the following
requirements:
->Number can start with +, - or . symbol.
For example:
✔+4.50
✔-1.0
✔.5
✔-.7
✔+.4
✖ -+4.5
->Number must contain at least 1 decimal value.
For example:
✖ 12.
✔12.0
->Number must have exactly one . symbol.
->Number must not give any exceptions when converted using float(N).
Input Format
The first line contains an integer T, the number of test cases.
The next T line(s) contains a string N.
Constraints
0 < T < 10
Output Format
Output True or False for each test case.
Sample Input 0
4
4.0O0
-1.00
+4.54
SomeRandomStuff
Sample Output 0
False
True
True
False
Input Format
The first line contains N and X separated by a space.
The next X lines contains the space separated marks obtained by students in
a particular subject.
Output Format
Print the averages of all students on separate lines.
The averages must be correct up to 1 decimal place.
Sample Input
53
89 90 78 93 80
90 91 85 88 86
91 92 83 89 90.5
Sample Output
90.0
91.0
82.0
90.0
85.5
Input Format
A single line of input contains the string S.
Constraints
0 < len(S) < 1000
Output Format
Output the sorted string S.
Sample Input
Sorting1234
Sample Output
ginortS1324
50. Rupal has a huge collection of country stamps. She decided to count the
total number of distinct country stamps in her collection. She asked for your
help. You pick the stamps one by one from a stack of N country stamps.
Find the total number of distinct country stamps.
Input Format
The first line contains an integer N, the total number of country stamps.
The next N lines contains the name of the country where the stamp is from.
Constraints
0 < N < 1000
Output Format
Output the total number of distinct country stamps on a single line.
Sample Input
7
UK
China
USA
France
New Zealand
UK
France
Sample Output
5
51. A newly opened multinational brand has decided to base their company logo
on the three most common characters in the company name. They are now
trying out various combinations of company names and logos based on this
condition. Given a string s , which is the company name in lowercase letters,
your task is to find the top three most common characters in the string.
Print the three most common characters along with their occurrence count.
Sort in descending order of occurrence count.
If the occurrence count is the same, sort the characters in alphabetical
order.
For example, according to the conditions described above,
GOOGLE would have it's logo with the letters G,O,E .
Input Format
A single line of input containing the string S.
Constraints
->3<len(S)<=10^4
->S has at least distinct characters
Output Format
Print the three most common characters along with their occurrence count
each on a separate line.
Sort output in descending order of occurrence count.
If the occurrence count is the same, sort the characters in alphabetical
order.
Sample Input 0
Aabbbccde
Sample Output 0
b3
a2
c2
52. You are given a string S. Suppose a character ‘c’ occurs consecutively X times
in the string. Replace these consecutive occurrences of the character 'c'
with (X,c) in the string.
For a better understanding of the problem, check the explanation.
Input Format
A single line of input consisting of the string S.
Output Format
A single line of output consisting of the modified string.
Constraints
All the characters of S denote integers between 0 and 9.
1<=|S|<=10^4
Sample Input
1222311
Sample Output
(1, 1) (3, 2) (1, 3) (2, 1)
Explanation
First, the character 1 occurs only once. It is replaced by (1,1). Then the
character 2 occurs three times, and it is replaced by (3,2) and so on.
53. You and Fredrick are good friends. Yesterday, Fredrick received N credit
cards from ABCD Bank. He wants to verify whether his credit card numbers
are valid or not. You happen to be great at regex so he is asking for your
help!
A valid credit card from ABCD Bank has the following characteristics:
Input Format
The first line of input contains an integer .
The next lines contain credit card numbers.
Constraints
0 < N < 100
Output Format
Print 'Valid' if the credit card number is valid. Otherwise, print 'Invalid'. Do
not print the quotes.
Sample Input
6
4123456789123456
5123-4567-8912-3456
61234-567-8912-3456
4123356789123456
5133-3367-8912-3456
5123 - 3567 - 8912 - 3456
Sample Output
Valid
Valid
Invalid
Valid
Invalid
Invalid
Input Format
The first line contains the number of items, .
The next lines contains the item's name and price, separated by a space.
Constraints
0 < N <=100
Output Format
Print the item_name and net_price in order of its first occurrence.
Sample Input
9
BANANA FRIES 12
POTATO CHIPS 30
APPLE JUICE 10
CANDY 5
APPLE JUICE 10
CANDY 5
CANDY 5
CANDY 5
POTATO CHIPS 30
Sample Output
BANANA FRIES 12
POTATO CHIPS 60
APPLE JUICE 20
CANDY 20
55. you are given two complex numbers, and you have to print the result of
their addition, subtraction, multiplication, division and modulus operations.
The real and imaginary precision part should be correct up to two decimal
places.
Input Format
One line of input: The real and imaginary part of a number separated by a
space.
Output Format
For two complex numbers C and D, the output should be in the following
sequence on separate lines:
-> C+D
->C-D
->C*D
->C/D
->mod( C )
->mod(D)
For complex numbers with non-zero real (A) and (B) complex part, the
output should be in the following format: A+Bi
Replace the plus symbol (+) with a minus symbol (-) when B<0.
For complex numbers with a zero complex part i.e. real numbers, the output
should be:
A+0.00i
For complex numbers where the real part is zero and the complex part is
non-zero, the output should be:
0.00+Bi
Sample Input
21
56
Sample Output
7.00+7.00i
-3.00-5.00i
4.00+17.00i
0.26-0.11i
2.24+0.00i
7.81+0.00i
56. You are given a string, and you have to validate whether it's a valid Roman
numeral. If it is valid, print True. Otherwise, print False. Try to create a
regular expression for a valid Roman numeral.
Input Format
A single line of input containing a string of Roman characters.
Output Format
Output a single line containing True or False according to the instructions
above.
Constraints
The number will be between 1 and 3999 (both included).
Sample Input
CDXXI
Sample Output
True
57. You are given some input, and you are required to check whether they are
valid mobile numbers.
A valid mobile number is a ten digit number starting with a 7,8 or 9.
Input Format
The first line contains an integer N , the number of inputs.
N lines follow, each containing some string.
Constraints
1<= N <=10
2<=len(Number)<=15
Output Format
For every string listed, print "YES" if it is a valid mobile number and "NO" if it
is not on separate lines. Do not print the quotes.
Sample Input
2
9587456281
1252478965
Sample Output
YES
NO
Output Format
Output True or False for each test case on separate lines.
Sample Input
3
5
12356
9
985632147
1
2
5
36541
7
1235689
3
982
Sample Output
True
False
False
59. You are given a list of N lowercase English letters. For a given integer K, you
can select any K indices (assume 1-based indexing) with a uniform
probability from the list.
Find the probability that at least one of the K indices selected will contain
the letter: 'a'.
Input Format
The input consists of three lines. The first line contains the integer N ,
denoting the length of the list. The next line consists of N space-separated
lowercase English letters, denoting the elements of the list.
The third and the last line of input contains the integer K, denoting the
number of indices to be selected.
Output Format
Output a single line consisting of the probability that at least one of the
K indices selected contains the letter:'a'.
Note: The answer must be correct up to 3 decimal places.
Constraints
1<=N<=10
I<=K<=N
All the letters in the list are lowercase English letters.
Sample Input
4
aacd
2
Sample Output
0.8333
Input Format
The first line contains an integer , the number of test cases.
The next lines contains an employee's UID.
Output Format
For each test case, print 'Valid' if the UID is valid. Otherwise, print 'Invalid',
on separate lines. Do not print the quotation marks.
Sample Input
2
B1CD102354
B1CDEF2354
Sample Output
Invalid
Valid
61. Ravi belongs to a very rich family which owns many gold mines. Today, he
brought N gold coins and decided to form a triangle using these coins. Isn't it
strange?
Ravi has a unusual way of forming a triangle using gold coins, which is
described as follows:
He puts 1 coin in the 1st row.
then puts 2 coins in the 2nd row.
then puts 3 coins in the 3rd row.
and so on as shown in the given figure.
Input Format
The first line of input will contain a single integer T, denoting the number of
test cases.
Each test case consists of 22 lines of input.
The first line of each test case contains a single integer N — the length of the
binary string.
The second line of each test case contains a binary string S of length N.
Output Format
For each test case, output on a new line — the minimum number of
operations required to sort the binary string.
Constraints
1≤T≤2⋅ 10^5
1≤N≤2⋅ 10^5
Sum of N over all test cases does not exceed 106106.
String S consists of only '00's and '11's.
Constraints
1≤T≤10^4
1≤N≤10^5
S contains only the characters 0 and 1.
Sum of N over all test cases does not exceed 2⋅ 10^5.
67. Hari wants to store some important numerical data on his personal
computer. He is using a new data type that can store values only
from 00 till N both inclusive. If this data type receives a value greater
than N then it is cyclically converted to fit into the range 00 to N. For
Example:
Value N+1 will be stored as 00.
Value N+2 will be stored as 11.
and so on...
Given X, the value chef wants to store in this new data type. Determine what
will be the actual value in memory after storing X.
Input Format
First line will contain T, number of testcases. Then the testcases follow.
Each testcase contains a single line of input, two space separated
integers N,X - the maximum value a data type can store and the value Chef
wants to store in the data type respectively.
Output Format
For each testcase, output in a single line the value which will be actually
stored in memory.
Constraints
1≤T≤3000
1≤N≤50
0≤X≤50
Constraints
1≤T≤100
1≤N≤100
71. Swathi is fan of pairs and he likes all things that come in pairs. He even has a
doll collection in which the dolls come in pairs. One day while going through
his collection he found that there are odd number of dolls. Someone had
stolen a doll!!!
Help Swathi find which type of doll is missing..
Input
The first line contains an integer T, the number of test cases.
The first line of each test case contains an integer N, the number of dolls.
The next N lines are the types of dolls that are left.
Output
For each test case, display the type of doll that doesn't have a pair, in a new
line.
Constraints
1<=T<=10
1<=N<=100000 (10^5)
0<=type<=100000
72. You are given an array A of size N. In one operation, you can do the
following:
Select indices i and j (I !=j) and set Ai =Aj .
Find the minimum number of operations required to make all elements of
the array equal.
Input Format
The first line of input will contain a single integer T, denoting the number of
test cases.
Each test case consists of multiple lines of input.
The first line of each test case contains an integer N — the size of the array.
The next line contains N space-separated integers, denoting the array A.
Output Format
For each test case, output on a new line, the minimum number of
operations required to make all elements of the array equal.
Constraints
1≤T≤1000
1≤N≤2⋅ 10^5
1≤Ai ≤N
The sum of N over all test cases won't exceed 2⋅ 10^5.
Devu despite being as rich as Gatsby, is quite frugal and can give at most one
grand party daily. Also, he wants to invite only one person in a party. So he
just wonders what is the maximum number of friendships he can save.
Please help Devu in this tough task !!
Input
The first line of the input contains an integer T denoting the number of test
cases. The description of T test cases follows.
First line will contain a single integer denoting n.
Second line will contain n space separated integers where ith integer
corresponds to the day dith as given in the problem.
Output
Print a single line corresponding to the answer of the problem.
Constraints
1 ≤ T ≤ 10^4
1 ≤ n ≤ 50
1 ≤ di ≤ 100
Constraints
1≤T≤500
1≤N≤1000
1≤Ai ≤N
Output Format
For each test case, output on a new line who takes the number home -
"Alice", "Bob", or "Charlie".
You may print each character in uppercase or lowercase. For
example, Alice, alice, aLiCe, and ALICE are all considered identical.
Constraints
1≤T≤100
1≤A≤1000
81. Kiran's phone has a total storage of S MB. Also, Chef has 2 apps already
installed on his phone which occupy X MB and Y MB respectively.
He wants to install another app on his phone whose memory requirement
is Z MB. For this, he might have to delete the apps already installed on his
phone. Determine the minimum number of apps he has to delete from his
phone so that he has enough memory to install the third app.
Input Format
The first line contains a single integer T — the number of test cases. Then
the test cases follow.
The first and only line of each test case contains four integers S,X,Y and Z —
the total memory of Kiran's phone, the memory occupied by the two already
installed apps and the memory required by the third app.
Output Format
For each test case, output the minimum number of apps Chef has to delete
from his phone so that he can install the third app.
Constraints
1≤T≤1000
1≤S≤500
1≤X≤Y≤S
X+Y≤S
Z≤S
The only condition for a woman to be eligible for the special training is that
she must be between 1010 and 6060 years of age, inclusive of
both 1010 and 6060.
Given the ages of N women in his village, please help San Te find out how
many of them are eligible for the special training.
Input Format
The first line of input contains a single integer T, denoting the number of
test cases. The description of T test cases follows.
The first line of each test case contains a single integer N, the number of
women.
The second line of each test case contains N space-separated
integers A1 ,A2 ,...,AN , the ages of the women.
Output Format
For each test case, output in a single line the number of women eligible for
self-defence training.
Constraints
1≤T≤20
1≤N≤100
1≤Ai ≤100
Find the maximum number of points Ajay can score if he optimally decides
the order of attempting both the problems.
Input Format
First line will contain T, number of test cases. Then the test cases follow.
Each test case contains of a single line of input, two integers X and Y - the
time required to solve problems A and B in minutes respectively.
Output Format
For each test case, output in a single line, the maximum number of points
Chef can score if he optimally decides the order of attempting both the
problems.
Constraints
1≤T≤1000
1≤X,Y≤100
Constraints
1≤T≤36
1≤A≤6
1≤B≤6
96. Arun has two variables X and Y. He wants to find out whether the variables
satisfy the equation:
X^2+4.Y^2=4.X^2.Y
Input Format
The first line of input will contain a single integer T, denoting the number of
test cases.
Each test case consists of two integers X and Y, as mentioned in statement.
Output Format
For each test case, output YES if the variables X and Y satisfy the given
equation, NO otherwise.
You may print each character in uppercase or lowercase. For
example, Yes, YES, yes, and YeS are all considered the same.
Constraints
1≤T≤1000
1≤X≤10^9
1≤Y≤10^18