Coding Session Question Bank-2
Coding Session Question Bank-2
(1) input X = 3
input Y = 4
Z=0
for i = 1 to X do
for j = 1 to Y do
Z=Z+i+j
output Z
1. 50 2. 54 3. 48 4. 60
(2) input X = 3
input Y = 4
Z=0
for i = 1 to X do
for j = 1 to Y do
Z=Z+i-j
output Z
2. -6 2. 4 3. -4 4. 0
(3) input P = 4
input Q = 2
R=0
for i = 1 to P do
for j = 1 to Q do
R = R + (i * j)
output R
1. 20 2. 28 3. 24 4. 30
(4) input M = 5
input N = 3
S=1
for i = 1 to M do
for j = 1 to N do
S = S + (i * j)
output S
1. 46 2. 61 3. 81 4. 91
(5) input X = 4
input Y = 3
Z=1
for i = 1 to X do
for j = 1 to Y do
Z = Z * (i + j)
output Z
(6) input N = 3
input M = 5
S=0
for i = 1 to N do
for j = 1 to M do
S=S-i-j
output S
(7) input X = 2
input Y = 4
Z=0
for i = 1 to X do
for j = 1 to Y do
Z = Z + (i * j) + j
Output Z
1. 24 2. 32 3. 50 4. 55
(8) input A = 3
input B = 3
C=0
for i = 1 to A do
for j = 1 to B do
C = C + (i + j)^2
output C
(9) input N = 4
input M = 2
R=0
for i = 1 to N do
for j = 1 to M do
R = R + (i - j)^2
output R
1. 14 2. 16 3. 18 4. 20
(10) input X = 4
input Y = 3
Z=1
for i = 1 to X do
for j = 1 to Y do
Z = Z * (i + j)
output Z
1.120 2. 72 3. 60 4. 90
In a sorted array, to find the maximum element, the time complexity in the worst
case is O(____).
The Worst Case complexity of finding the maximum element in an unsorted array is:
1. O(n) 2. O(log n) 3. O(n^2) 4. O(1)
The time complexity of reversing a string is:
1. O(n) 2. O(log n) 3. O(n^2) 4. O(1)
The time complexity of concatenating two strings is: if sl and s2 length is n and m
1. O(n + m) 2. O(n *m) 3. O(log nm) 4. O(1)
The Worst Case time complexity of finding the maximum element in an array is:
1. O(n) 2. O(log n) 3. O(n^2) 4. O(1)
1. 6 2. 9 3. 15 4. Error
1. h 2. O 3. W 4. Error
1. 1 2. 2 3. 3 4. Error
Consider the following string: str = "programming".
What is the character at index 4?
1. P 2. R 3. A 4. M
1. C 2. O 3. M 4. p
1. 3 2. 4 3. 5 4. 2
Consider the following array: arr = [10, 20, 30, 40, 50].
What will be the output of arr[2]?
1. 30 2. 40 3. 20 4. 50
1. h 2. e 3. l 4. o
1. 1 2. 4 3. 3 4. 5
1. 7 2. 10 3. 11 4. 9
1. s 2. t 3. a 4. e
Consider the following string: str = "openai".
What will be the output of str.charAt(3)?
1. o 2. e 3. n 4. a
Given the string str = "data science", what character will be on the 7th index?
1. a 2. s 3. e 4. i
1. a 2. s 3. e 4. i
Find output
1.
int[] arr = {10, -22, 35, -40, 20};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
result += arr[i];
} else {
result -= arr[i];
}
}
print(result);
a) 87 b) 107 c) 67 d) 97
2.
int[] arr = {5, 10, 15, 20, 25};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0) {
result += arr[i];
} else {
result -= arr[i];
}
}
print(result);
a) 20 b) 10 c) 15 d) 25
3.
int[] arr = {2, 3, 4, 5, 6};
int result = 1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0) {
result *= arr[i];
} else {
result += arr[i];
}
}
print(result);
4.
int[] arr = {10, 20, 30, 40, 50};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (i == 2 || i == 3 || i == 5) {
result += arr[i];
}
}
print(result);
a) 70 b) 50 c) 60 d) 40
5.
a) 3 b) 4 c) 2 d) 5
6.
int[] arr = {9, 15, 22, 18, 7};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 3 == 0) {
result += arr[i];
}
}
print(result);
a) 42 b) 50 c) 24 d) 60
7.
int[] arr = {10, 22, 35, 40, 20};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (i % 10 == 0) {
result += arr[i];
} else {
result -= arr[i];
}
}
print(result);
1. 13 2. 23 3. 14 4. 18
8.
int[] arr = {8, 3, 5, 7, 10};
int max = Integer.MIN_VALUE;
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0 && arr[i] > max) {
max = arr[i];
}
}
print(max);
a) 8 b) 5 c) 10 d) 7
9.
int[] arr = {30, 15, 40, 10, 20};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 25) {
result -= arr[i];
}
}
print(result);
a) -70 b) -60 c) -40 d) -50
10.
int[] arr = {2, 3, 4};
int result = 0;
for (int i = 0; i < arr.length; i++) {
result += arr[i] * arr[i];
}
print(result);
a) 29 b) 25 c) 35 d) 30
11.
int[] arr = {8, 12, 5, 9, 16};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 4 != 0) {
result += arr[i];
}
}
print(result);
a) 14 b) 26 c) 21 d) 23
12.
int[] arr = {2, 0, 5, 3, 0};
int result = 1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != 0) {
result *= arr[i];
}
}
System.out.println(result);
a) 20 b) 30 c) 10 d) 25
13.
int[] arr = {11, 24, 35, 48, 59};
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0) {
count++;
}
}
print(count);
a) 3 b) 2 c) 5 d) 4
14.
int[] arr = {10, 20, 30, 40, 50, 60};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (i % 3 == 0) {
result += arr[i];
}
}
print(result);
a) 90 b) 70 c) 60 d) 50
15.
int[] arr = {10, 20, 30, 40, 50};
int result = 0;
int lastElement = arr[arr.length - 1];
for (int i = 0; i < arr.length; i++) {
result += arr[i] - lastElement;
}
System.out.println(result);
a) -100 b) -60 c) -50 d) -40
16.
int[] arr = {8, 3, 2, 5};
double result = 1.0;
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0) {
result *= arr[i];
}
}
System.out.println(result);
a) 16.0 b) 24.0 c) 8.0 d) 12.0
1. Write a program to find the minimum and maximum element from an array.
Input: arr = [15, 3, 1, 6, 5, 8]
Output: 1 and 15
2. Write a program to find the sum of all even numbers from an array.
Input: arr = [15, 31, 1, 6, 4, 30]
Output: 40
3. Write the intuition and code of the Bubble Sort algorithm.
4. Write the intuition and code of the Selection Sort algorithm.
5. Write the code of the Binary Search algorithm and explain its time complexity.
6. Write a program to find the duplicate elements from an array.
Input: arr = [5, 3, 1, 6, 5, 8]
Output: 5
7. Write a program to find the count of vowels and consonants in a given string.
Input: String = "krmangalamuniversity"
Output: Vowels: 7, Consonants: 13
8. Write a program to reverse a given string.
Input: String = "KRMU"
Output: "UMRK"
9. Write a program to find the count of odd numbers in a given array.
Input: arr = [1, 3, 5, 7, 8, 10]
Output: 4
10. Write a program to remove all duplicate characters from a string.
Input: String = "programming"
Output: "progamin"
11.Write a program to check if a given string is a palindrome.
Input: String = "madam"
Output: True
12. Write a program to find the common elements in two arrays.
Input: arr1 = [1, 2, 3, 4], arr2 = [3, 4, 5, 6]
Output: [3, 4]
13. Find the Missing Number in an Array
Problem Statement:
You are given an array of integers, arr, that contains n-1 distinct numbers
ranging from 1 to n. The numbers in the array are unique and lie within the range
Pattern Printing
Loops
6. Write a program to print the prime numbers between 1 and n using loops.
Input: n = 50
Output: [2, 3, 5, 7, 11, 13, ...]
7. Write a program to find the factorial of a number using loops.
Input: n = 5
Output: 120
8. Write a program to generate the first n terms of the look-and-say sequence.
Input: n = 5
Output: 1, 11, 21, 1211, 111221
9. Write a program to calculate the sum of the series:
S=1+1/2+1/3+...+1/nS=1+1/2+1/3+...+1/n
Input: n = 5
Output: 2.28333
10. Write a program to find the LCM of two numbers using loops.
Input: a = 12, b = 18
Output: 36
Conditionals
Binary Search
16. Write a program to find the square root of a number using binary search.
Input: n = 16
Output: 4
17. Write a program to find the peak element in an array (an element greater than
its neighbors) using binary search.
Input: arr = [1, 3, 20, 4, 1, 0]
Output: 20
18. Write a program to search for the first occurrence of a target element in a
sorted array using binary search.
Input: arr = [2, 4, 10, 10, 10, 18], target = 10
Output: 2
19. Write a program to find the smallest element in a rotated sorted array using
binary search.
Input: arr = [7, 9, 1, 3, 5]
Output: 1
20. Write a program to count the occurrences of a target element in a sorted array
using binary search.
Input: arr = [1, 2, 2, 2, 3, 4], target = 2
Output: 3
21. Write a program to find the longest palindromic substring in a given string.
Input: s = "babad"
Output: "bab"
22. Write a program to check if a string is a valid anagram of another string,
ignoring spaces and case.
Input: s1 = "Listen", s2 = "Silent"
Output: True
23. Write a program to find the first non-repeating character in a string.
Input: s = "swiss"
Output: "w"
24. Write a program to compress a string using the counts of repeated characters.
Input: s = "aaabbcc"
Output: "a3b2c2"
25. Write a program to find the longest substring without repeating characters.
Input: s = "abcabcbb"
Output: "abc"
26. Write a program to determine if one string is a rotation of another string.
Input: s1 = "waterbottle", s2 = "erbottlewat"
Output: True
27. Write a program to convert a given string to a zigzag pattern and return the
string in row-wise order.
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
28. Write a program to check if two strings are permutations of each other.
Input: s1 = "test", s2 = "tset"
Output: True
29. Write a program to group anagrams from a list of strings.
Input: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
Output: [["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]
30. Write a program to calculate the edit distance (Levenshtein distance) between
two strings.
Input: s1 = "kitten", s2 = "sitting"
Output: 3
Problem: Given an array of integers and a target value k, find the number of subarrays
Explanation: Subarrays are [4, 2], [2, 6], [6], and [4, 2, 2, 6].
Input:
arr = [1, 2, 3, 4, 5, 6], k = 2
Output:
[5, 6, 1, 2, 3, 4]
Problem: Find the maximum product that can be obtained from any contiguous subarray
of a given array.
Input:
arr = [2, 3, -2, 4, -1]
Output:
48
Problem: Given an array of integers and an integer k, find the length of the longest
Problem: Find the minimum length of a subarray with a sum greater than a given value k.
Input:
arr = [1, 10, 5, 2, 7], k = 9
Output:
1
Problem: Find the number of inversions in an array, where an inversion is a pair (i, j)
Explanation: The inversions are (8, 4), (8, 2), (8, 1), (4, 2), (4, 1), (2,
1).
Problem: Find the length of the longest subarray that is first increasing and then
decreasing.
Input:
arr = [12, 4, 78, 90, 45, 23]
Output:
5
Explanation: The subarray [4, 78, 90, 45, 23] is the longest bitonic subarray.
Problem: Find the maximum difference between two elements in the array such that the
Problem: Sort an array containing only 0s, 1s, and 2s in linear time without using any
sorting algorithm.
Input:
arr = [0, 1, 2, 1, 2, 0, 0, 1]
Output:
[0, 0, 0, 1, 1, 1, 2, 2]
Problem: Find the maximum sum of a subarray after modifying at most one element of the
Problem: Given an unsorted array, find the smallest missing positive integer.
Input:
arr = [3, 4, -1, 1]
Output:
2
Problem: Find the contiguous subarray of size k that has the maximum average.
Input:
arr = [1, 12, -5, -6, 50, 3], k = 4
Output:
12.75
Explanation: The subarray [12, -5, -6, 50] has the maximum average.
Problem: Rearrange an array such that the sum of absolute differences between adjacent
elements is maximized.
Input:
arr = [1, 2, 4, 8]
Output:
[8, 1, 4, 2]
16. Merge Intervals
Input:
intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]
Output:
[[1, 6], [8, 10], [15, 18]]
Problem: Find the minimum number of platforms needed for a railway station so no train
waits.
Input:
arrivals = [900, 940, 950, 1100, 1500, 1800]
Problem: Check if the array can be divided into pairs such that the sum of every pair is
divisible by k.
Input:
arr = [9, 7, 5, 3], k = 6
Output:
True
Advance level array problems
Problem: Given an array, determine if it can be split into two subarrays such that the sum
of both subarrays is equal. If possible, return the index where the split occurs; otherwise,
return -1.
Input:
arr = [1, 2, 3, 4, 6]
Output:
3
Explanation: Splitting the array at index 3 gives [1, 2, 3] and [4, 6], both of
which sum to 6.
Problem: Find all the subarrays in a given array that have a sum of 0.
Input:
arr = [3, 4, -7, 1, 2, -1, 6]
Output:
[[3, 4, -7], [4, -7, 1, 2], [-7, 1, 2, -1]]
Problem: Given an array, rotate it to maximize the sum of i * arr[i] for all valid i.
Input:
arr = [1, 2, 3, 4]
Output:
20
Problem: Find the length of the longest subsequence in an array such that the elements
are consecutive integers. The sequence can be unordered.
Input:
arr = [1, 9, 3, 10, 4, 20, 2]
Output:
4
Problem: Find the element that appears more than n/2 times in an array, where n is the
size of the array. Assume that such an element always exists.
Input:
arr = [3, 3, 4, 2, 3, 3, 1]
Output:
3
Problem: Given an array of positive integers, count the number of contiguous subarrays
where the product of all elements is less than k.
Input:
arr = [10, 5, 2, 6], k = 100
Output:
8
10. Smallest Subarray with All Occurrences of the Most Frequent Element
Problem: Find the smallest subarray that contains all occurrences of the most frequent
element in the array.
Input:
arr = [1, 2, 2, 3, 1, 4, 2]
Output:
[2, 2, 3, 1, 4, 2]
11. Array Transformation
Problem: Transform an array such that arr[i] becomes the difference between the
sum of the elements on the left and the sum of the elements on the right.
Input:
arr = [4, 3, 7, 1]
Output:
[-11, -8, 0, 14]
Problem: Count the number of pairs in an array whose XOR equals a given number k.
Input:
arr = [5, 4, 10, 15, 7, 6], k = 5
Output:
2
Problem: Find the maximum distance between two occurrences of the same element in
the array.
Input:
arr = [3, 5, 4, 3, 5, 3, 4]
Output:
6
14. Maximum Absolute Difference Between Left and Right Sums
Problem: Split an array into two parts and return the maximum absolute difference
between the sum of the left part and the sum of the right part.
Input:
arr = [2, 4, -3, 6, 7, 1]
Output:
14
Problem: Rearrange the array so that the resulting concatenated string of all elements is
lexicographically largest.
Input:
arr = [54, 546, 548, 60]
Output:
[60, 548, 546, 54]