0% found this document useful (0 votes)
144 views

Strings

Here are the steps to solve this problem: 1. Find all prefixes of the given string. 2. Find all suffixes of the given string. 3. Compare each prefix with each suffix to check for matching strings. 4. The length of the longest matching prefix/suffix is the output. If no match is found, output should be -1. Input: "xxAbcxxAbcxx" Output: 2 Explanation: The longest common prefix and suffix is "cx". Its length is 2.

Uploaded by

navi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views

Strings

Here are the steps to solve this problem: 1. Find all prefixes of the given string. 2. Find all suffixes of the given string. 3. Compare each prefix with each suffix to check for matching strings. 4. The length of the longest matching prefix/suffix is the output. If no match is found, output should be -1. Input: "xxAbcxxAbcxx" Output: 2 Explanation: The longest common prefix and suffix is "cx". Its length is 2.

Uploaded by

navi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Question 1

Given a string of odd length greater 7, return a string made of the middle three
chars of a given String
For example: –
getMiddleThreeChars("JhonDipPeta") → "Dip"
getMiddleThreeChars("Jasonay") → "son“

Expected Output:

Original String is JhonDipPeta


Middle three chars are Dip
Original String is Jasonay
Middle three chars are son
Question 2
Arrange String characters such that lowercase letters should come first
Given input String of combination of the lower and upper case arrange
characters in such a way that all lowercase letters should come first.

Input:
PyNaTive
Output:
arranging characters giving precedence to lowercase letters:
yaivePNT
Question 3
Concatenate two lists index-wise

Input:
list1 = ["M", "na", "i", "Ke"]
list2 = ["y", "me", "s", "lly"]

Expected output:
['My', 'name', 'is', 'Kelly']
Question 4
Input - A string of numbers
Output - Check if the number is palindrome. If it is, print it's length.
If not, ( get the reverse of the string, add it to the original string, and check
again). Repeat until a palindrome is generated.

Example Test cases -


1. Input - 145,
Output - 3.
#145 is not palindrome. Add 145+541=686, which is a palindrome. Hence print
it's length, 3

2. Input - 4,
Output - 1.
# 4 is a palindrome.
Question 5
Little Robert likes mathematics. Today his teacher has given him two integers
and asked to find out how many integers can divide both the numbers. Would
you like to help him in completing his school
assignment?
Input Formatting: There is 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 value 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.
Question 6
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: First input will be an integer T. It depicts a number of test cases.
Followed by value for each test cases. 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”.
Question 6
Input:
2
2
3

Output:
YEAH
NAH

Explanation:
The first input is a number of test cases.
Iterate to read all the inputs and store it in the Python list.
For each element in the list, calculate prod and sum.
Print “YEAH” if the prod is divided by the sum. Otherwise, print “NAH”.
PROBLEM STATEMENT 7
Given an integer N
N<=10^50
Output the no. of pairs (x,y) such that
0<=x<=n
0<=y<=n
F(x) + F(y) = Prime number
Note : (x,y) and (y,x) are to be treated as same pair
Example
Input
3
Output
5
Explanation
5 pairs (0,2), (1,1), (0,3), (2,3), (1,2) give prime no.s
PROBLEM STATEMENT 8
Given a string, count all distinct substrings of the given string.
Example
Input
abcd
Output
10
All Elements are Distinct
Input
aaa
Output
3
PROBLEM STATEMENT 9
In Hackerland every character has a weight. The weight of an English uppercase alphabet A-
Z is given below :
A=1
B = 2*A + A
C = 3*B + B
D = 4*C + C
….
Z = 26*Y + Y
The weight made up of these characters is the summation of weights of each character.
Given a total string weight, determine shortest string of given weight. If there is more than
one solution, return the lexicographically smallest of them. For example, given weight = 25,
and the weights of the first few characters of the alphabets are A=1, B=3, C=12, D=60 it is
certain that no letter larger than C is required. Some of the strings with a total weight equal to
the target are ABBBBC, ACC, AAAAAAABBBBBB. The shortest of these is ACC. While any
permutation of these characters will have same weight, this is the lexicographically smallest
of them.
PROBLEM STATEMENT 9
Example
Input
20
Output
AABBC
PROBLEM STATEMENT 10
Consider a string, group the similar characters in combinations. Then, concatenate
first element and last element alternatively.
For instance, Consider a string “HelLoWOrld”, combinations of similar characters will
be:
[‘d’, ‘e’, ‘H’, ‘lLl’, ‘oO’, ‘r’, ‘W’]
So, final output : dWerHoOlLl
Input:
First Input : s, string
Output:
String [Manipulated]
Constraints:
1 ≤ T ≤ 70
2 < N ≤ 10000
PROBLEM STATEMENT 10

Test Cases:

Sample Input Sample Output


HelLoWOrld dWerHoOlLl
PROBLEM STATEMENT 11
Given m*n matrix where m defines number of rows and n defines number of
columns. Your job is to check whether a number is consecutive for 3 times either in
row, column, or diagonal. If there are more than one such numbers then print the
minimum one.
Note: n = m+1
Input:
First Input : m, displaying number of rows
Second Input : m*n matrix
Output:
Integer
Constraints:
1 ≤ T ≤ 200
2 ≤ N ≤ 10000
PROBLEM STATEMENT 11

Test Cases:

Sample Input Sample Output


23456243 1
23476762
23555525
23112136
11119035
23115127
PROBLEM STATEMENT 12
Given a special set of numbers and a special sum. Find all those combinations from the
given set, equaling the given sum
Input Format :
First Input : Set of numbers
Second Input : Special Sum
Output Format :
Combinations satisfying criteria
Test Cases:
INPUT OUTPUT
-1, 1, 0, 0, 2, -2 3

0
PROBLEM STATEMENT 13
Given a row/column count and a matrix, your job is to find those possible 2*2 matrix
where each should follow the given rule :
Each element of matrix should be divisible by sum of its digits.
Input Format :
First Input : Row count, Column Count
Second Input : Matrix
Output Format :
2*2 matrices satisfying the rule.
PROBLEM STATEMENT 13

Test Cases:

Sample Input Sample Output


43 40 42
40 42 2 30 24
30 24 27
180 190 40 42 2
24 27
11 121 13
30 24
180 190

24 27
190 40
PROBLEM STATEMENT 14
You are given a list of numbers from 1 to 9, in which each number is separated by ‘,’.
Your job is to find the sum of two numbers. These two numbers are needed to be
calculated as per following rules : 
1. First number should be calculated as :
Add all the numbers that do not come between  4 and 7 in the input.
2.  Second number should be calculated as :
Append all the numbers to each other that comes between 4 and 7 (inclusive).
Find the sum of both calculated numbers.
Note : 4 always comes before 7.
Input : 
First Input : List of numbers
Output : 
Sum of both calculated numbers
PROBLEM STATEMENT 14
Test Cases:

Sample Input Sample Output

1,3,4,6,8,7,2,9 4702
PROBLEM STATEMENT 15
Given a list of numbers and a special sum. Find all those combinations from the
given list and print the count of combinations whose sum is equal to the given sum.
Input : 
First Input : list of numbers
Second Input : Sum value
Output : 
Count of Combinations satisfying criteria
Test Cases:
Sample Input Sample Output
1,4,2,1,3,2 4
8
PROBLEM STATEMENT 16
Write a python program that it should consist of special char, numbers and chars .
1) If there are even numbers of special chars then:
the series should start with even followed by odd
Input: t9@a42&516
Output: 492561
2) If there are odd numbers of special chars then the output will be starting with odd followed by
Even:
Input: 5u6@25g7#@
Output: 56527

If there are any number of additional digits append them at last


PROBLEM STATEMENT 17
A non empty str containing only alphabets . print the longest prefix in str which
is same as suffix.

Prefix and suffix should not be overlapped


Print -1 if no prefix exits which is also the suffix without suffix without overlap

Do case sensitive comparison wherever necessary


Position start from 1.

Input: “xxAbcxxAbcxx”
Output: 2

You might also like