Getting ready for a coding interview? Hone your skills and practice answering technical questions with ten sample problems — and their solutions — from InterviewBit below!
Problem one
Description
- Panagram is the string that contains all 26 letters of the alphabet (irrespective of the case). As a programmer you have to check whether the string taken as input is panagram or not.
Input format
- The first line takes the input t denoting the number of testcases.
- The following t lines takes the input for each of the corresponding strings.
Output format
- Print the result as YES if the string is panagram and NO if the string is not a panagram in t separate lines corresponding to all the t testcases.
Sample input
abcdefghijkcflmnopqrscceftuvwxrcrevrejnvibyz
ecedccncnecrekcercervcerv
Sample output
NO
Solution: Python
t=int(input())
for i in range(t):
s=input()
s=s.lower()
s1=set()
for j in range(len(s)):
s1.add(s[j])
if(len(s1)>=26):
print(“YES”)
else:
print(“NO”)
Problem two
Description
- List down all the album titles by artist named Metallica and the count of tracks in each Album.
- Make sure you list them in increasing order of AlbumId.
Sample output
11
Note
- You can run multiple queries at once and see their output in the output section in the order the queries were written.
- The Sample Output is for illustrative purposes only. This may/may not reflect actual data.
Solution: SQL
SELECT al.title,
Count(*)
FROM album al
JOIN artist at
ON al.artistid = at.artistid
JOIN track t
ON al.albumid = t.albumid
WHERE at.name = ‘metallica’
GROUP BY al.title,al.albumid order by al.albumid;
Problem three
Description
- Given a positive integer A, return an array of strings with all the integers from 1 to N.
- But for multiples of 3 the array should have “Fizz” instead of the number.
- For the multiples of 5, the array should have “Buzz” instead of the number.
- For numbers which are multiple of 3 and 5 both, the array should have “FizzBuzz” instead of the number.
- Look at the example for more details.
Example
- A = 5
- Return: [1 2 Fizz 4 Buzz]
Solution: Javascript
function fizzBuzz(n) {
let result = [];
for (let i = 1; i <= n; i++) {
if (i % 3 === 0 && i % 5 === 0) {
result.push(“FizzBuzz”);
} else if (i % 3 === 0) {
result.push(“Fizz”);
} else if (i % 5 === 0) {
result.push(“Buzz”);
} else {
result.push(i.toString());
}
}
return result;
}
Problem four
Description
- Given two integers A and B, where A is the first element of the sequence then find Bth element of the sequence.
- If the kth element of the sequence is X then k+1th element calculated as:
- If X is even then next element is X/2
- Else next element is 3×X + 1
Problem constraints
- 1 <= A <= 109
- 1 <= B <= 105
Input format
- Given two integers A and B
Output format
- Return an integer
Sample input
A = 1
B = 3
Input 2:
A = 5
B = 6
Sample output
2
Output 2:
1
Sample explanation
The sequence is as follows 1 -> 4 -> 2
Explanation 2:
The sequence is as follows 5 -> 16 -> 8 -> 4 -> 2 -> 1
Solution: Javascript
function collatz_sequence(A, B) {
let x = A;
for (let i = 1; i < B; i++) {
if (x % 2 === 0) {
x = x / 2;
} else {
x = 3 * x + 1;
}
}
return x;
}
Problem five
Description
- You are given a string A of size N.
- Return the string A after reversing the string word by word.
Note
- A sequence of non-space characters constitutes a word.
- Your reversed string should not contain leading or trailing spaces, even if it is present in the input string.
- If there are multiple spaces between words, reduce them to a single space in the reversed string.
Problem constraints
- 1 <= N <= 3 * 105
Input format
- The only argument given is string A
Output format
- Return the string A after reversing the string word by word
Sample input
A = “the sky is blue”
Input 2:
A = “this is ib”
Sample output
“blue is sky the”
“ib is this”
Sample explanation
We reverse the string word by word so the string becomes “the sky is blue”.
Explanation 2:
We reverse the string word by word so the string becomes “this is ib”.
Solution: Javascript
function reverseWords(A) {
// Step 1: Split the string into words
const words = A.trim().split(/\s+/);
// Step 2: Reverse the order of the words and join them together
return words.reverse().join(” “);
}
Problem six
Description
- Given an integer A.
- Write binary representation of the integer without leading zeros.
- Flip all bits then return the integer value of the binary number formed.
- Flipping means 0 -> 1 and 1 -> 0.
Problem constraints
- 1 <= A <= 109
Input format
- Given an integer A.
Output format
- Given an integer A.
Sample input
A = 7
A = 5
Sample output
0
Output 2:
2
Sample explanation
7 -> 111 -> 000 ->0
Explanation 2:
5 -> 101 -> 010 ->3
Solution: Javascript
function flipBits(A) {
// Step 1: Convert A into binary representation
let binaryStr = A.toString(2);
// Step 2: Remove the prefix “0b” from the binary string
binaryStr = binaryStr.slice(2);
// Step 3: Remove leading zeros from the binary string
binaryStr = binaryStr.replace(/^0+/, ”);
// Step 4: Flip all the bits in the binary string
let flippedStr = ”;
for (let i = 0; i < binaryStr.length; i++) {
flippedStr += (binaryStr[i] === ‘0’) ? ‘1’ : ‘0’;
}
// Step 5: Convert the resulting binary string into an integer
let flippedInt = parseInt(flippedStr, 2);
return flippedInt;
}
Problem seven
Description
- Given an integer array A of size N.
- You have to pick exactly B elements from either left or right end of the array A to get the maximum sum.
- Find and return this maximum possible sum.
Note
- Suppose B = 4 and array A contains 10 elements then:
- You can pick the first four elements or can pick the last four elements or can pick 1 from the front and 3 from the back etc. you need to return the maximum possible sum of elements you can pick.
Problem constraints
- 1 <= N <= 105
- 1 <= B <= N
- -103 <= A[i] <= 103
Input format
- First argument is an integer array A.
- Second argument is an integer B.
Output format
- Return an integer denoting the maximum possible sum of elements you picked.
Sample input
A = [5, -2, 3 , 1, 2]
B = 3
Input 2:
A = [1, 2]
B = 1
Sample output
8
Output 2:
2
Solution: Python
def max_sum(A, B):
n = len(A)
max_sum_left = sum(A[:B]) # maximum sum of B elements from the left end
max_sum_right = sum(A[-B:]) # maximum sum of B elements from the right end
max_sum = max(max_sum_left, max_sum_right)
for i in range(1, B):
sum_left = sum(A[:i]) # sum of i elements from the left end
sum_right = sum(A[-(B–i):]) # sum of B–i elements from the right end
max_sum = max(max_sum, sum_left+sum_right)
return max_sum
Problem eight
Description
- Given the position of a Bishop (A, B) on an 8 * 8 chessboard.
- Your task is to count the total number of squares that can be visited by the Bishop in one move.
- The position of the Bishop is denoted using row and column number of the chessboard.
Problem constraints
- 1 <= A, B <= 8
Input format
- First argument is an integer A denoting the row number of the bishop.
- Second argument is an integer B denoting the column number of the bishop.
Output format
- Return an integer denoting the total number of squares that can be visited by the Bishop in one move.
Sample input
B = 4
Sample output
Solution: Python
A Bishop can move diagonally on the chessboard. In one move, it can move to any square that is diagonally adjacent to its current position. We need to count the total number of such squares that can be reached by the Bishop in one move.
Let’s consider the position of the Bishop as (x, y) on the chessboard. There are four diagonals on the chessboard that pass through this position. We need to count the number of squares on each diagonal that can be reached by the Bishop in one move.
For each diagonal, we can calculate the number of squares that lie on it and are reachable by the Bishop in one move. To do this, we can count the number of squares on the diagonal that are in the same row or column as the Bishop, and the number of squares that are between the Bishop and the edge of the chessboard.
Let’s take an example. Suppose the Bishop is at position (4, 4). The four diagonals passing through this position are:
- (1, 1), (2, 2), (3, 3), (5, 5), (6, 6), (7, 7), (8, 8)
- (1, 7), (2, 6), (3, 5), (5, 3), (6, 2), (7, 1)
- (8, 1), (7, 2), (6, 3), (4, 5), (3, 6), (2, 7), (1, 8)
- (8, 7), (7, 6), (6, 5), (5, 4), (3, 2), (2, 1)
On the first diagonal, there are 3 squares to the left of the Bishop and 4 squares to the right of the Bishop, for a total of 7 squares. Similarly, we can count the squares on the other three diagonals. The total number of squares that can be reached by the Bishop in one move is the sum of the squares on all four diagonals.
The Python code to implement this solution is given below:
def countSquares(A, B):
# Count squares on each diagonal
diag1 = min(A-1, B-1) + min(8-A, 8-B)
diag2 = min(A-1, 8-B) + min(8-A, B-1)
diag3 = min(8-A, B-1) + min(A-1, 8-B)
diag4 = min(8-A, 8-B) + min(A-1, B-1)
# Total squares reachable by Bishop
total = diag1 + diag2 + diag3 + diag4
return total
We use the min function to calculate the number of squares on each diagonal. The min(A-1, B-1) gives the number of squares to the left of the Bishop on the first diagonal, and min(8-A, 8-B) gives the number of squares to the right of the Bishop. We add these two to get the total number of squares on the diagonal. Similarly, we count the squares on the other three diagonals. Finally, we add up the squares on all four diagonals to get the total number of squares that can be reached by the Bishop in one move.
Problem nine
Description
- Given a sorted array A of size N. Find number of elements which are less than or equal to B.
Note
- Expected Time Complexity O(log N)
Problem constraints
- 1 <= N <= 106
- 1 <= A[i], B <= 109
Input format
- First argument is an integer array A of size N.
- Second argument is an integer B.
Output format
- Return an integer denoting the number of elements which are less than or equal to B.
Sample input
A = [1, 3, 4, 4, 6]
B = 4
Input 2:
A = [1, 2, 5, 5]
B = 3
Sample output
4
Output 2:
2
Solution: Python
Algorithm:
- Initialize start = 0 and end = N-1.
- While start <= end, do the following: a. Compute mid as (start + end) / 2. b. If A[mid] <= B, update start = mid + 1. c. Otherwise, update end = mid – 1.
- Return start.
Code:
def count_elements_less_than_equal_to_B(A, B):
start = 0
end = len(A) – 1
while start <= end:
mid = (start + end) // 2
if A[mid] <= B:
start = mid + 1
else:
end = mid – 1
return start
Problem ten
Description
- You are climbing a stair case and it takes A steps to reach to the top.
- Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Problem constraints
- 1 <= A <= 36
Input format
- The first and the only argument contains an integer A, the number of steps.
Output format
- Return an integer, representing the number of ways to reach the top.
Sample input
2 Explanation 1:
[1, 1], [2] Input 2:
A = 3 Output 2:
3 Explanation 2:
[1 1 1], [1 2], [2 1]
/**
* @input A : Integer
*
* @Output Integer
*/
int climbStairs(int A) {
int count[A+1], i;
count[0] = 0;
count[1] = 1;
count[2] = 2;
for (i = 3; i <= A; ++i) {
count[i] = count[i-1] +
count[i-2];
}
return count[A];
}
Attending a Climb partner bootcamp and ready to get to the next stage in your career? Sign up for our free ClimbTalent career development platform to access job listings, resources and tools, mentorships, and more!
*Sample problems and solutions from https://www.interviewbit.com/practice/.