Top Deloitte Coding Questions With Answers Assessment by Expert
Top Deloitte Coding Questions With Answers Assessment by Expert
Answers
m
No. of Questions For The Coding Round 2 Live Coding Questions
.co
Time Duration 90 Minutes
er
Top 10 Deloitte Coding Questions with Answers
rn
Here, we’ve listed some commonly asked Deloitte coding questions with answers:
Write a Program
Answer:
el
Sample Input: 34
od
C++ Code:
#include <iostream>
using namespace std;
Cc
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
m
int main() {
int num = 34;
findPrimeSum(num);
.co
return 0;
}
Java Code:
er
public class PrimeSum {
return true;
}
+ ".");
found = true;
break; // Uncomment this to find all pairs
}
Cc
}
if (!found)
System.out.println(num + " cannot be expressed as the sum of two prime numbers.");
}
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def find_prime_sum(n):
m
for i in range(2, n//2 + 1):
if is_prime(i) and is_prime(n - i):
return f"{n} can be expressed as the sum of {i} and {n - i}."
return f"{n} cannot be expressed as the sum of two prime numbers."
.co
# Sample input
n = 34
find_prime_sum(n)
Output:
er
rn
'34 can be expressed as the sum of 3 and 31.'
Write a Program
el
Answer:
Sample Input: 28
od
C++ Code:
#include <iostream>
Cc
bool isPerfect(int n) {
int sum = 1;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
sum += i;
if (i != n / i) sum += n / i;
}
}
return sum == n && n != 1;
}
int main() {
int n = 28; // Sample input
if (isPerfect(n))
cout << n << " is a perfect number." << endl;
else
cout << n << " is not a perfect number." << endl;
return 0;
}
m
Java Code:
.co
static boolean isPerfect(int n) {
int sum = 1;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
sum += i;
}
er
}
if (i != n / i) sum += n / i;
rn
return sum == n && n != 1;
}
Python Code:
def is_perfect(n):
sum = 1
Cc
# Sample input
n = 28
if is_perfect(n):
result = f"{n} is a perfect number."
else:
result = f"{n} is not a perfect number."
result
Output:
28 is a perfect number because its divisors are 1, 2, 4, 7, and 14, and the sum of these divisors
is 28.
m
Q.3 Problem Statement:
.co
There is a list of decimal numbers, as well as an infection that has a specific amount of
spikes. Each spike represents the amount of binary digits that a virus will eat from the
right-hand part of a decimal number. Write a program that will determine the final status
of each number following the virus that has consumed the specified amount of binary
er
digits from each one of them.
Answer:
rn
Sample Input:
ea
6
10 20 30 40 50 60
3
el
C++ Code:
od
#include <iostream>
#include <vector>
using namespace std;
int main() {
Cc
int N, n;
cin >> N;
vector<int> v(N);
for (int i = 0; i < N; i++)
cin >> v[i];
cin >> n;
for (auto i : v)
cout << (i >> n) << " ";
return 0;
}
Java Code:
import java.util.Scanner;
m
}
int n = sc.nextInt();
for (int i : arr) {
System.out.print((i >> n) + " ");
.co
}
}
}
Python Code:
er
N = int(input())
a = list(map(int, input().split()))
n = int(input())
rn
result = " ".join(str(i >> n) for i in a)
print(result)
ea
Output:
‘1 2 3 5 6 7’
el
od
Q.4 Can You Write a program that takes a string containing multiple
words as input? Sort these words in ascending order alphabetically.
Then, print all the words that start with a vowel, along with their
Cc
Answer:
C++ Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
}
int main() {
string input;
m
getline(cin, input);
vector<string> words;
string word = "";
.co
for (char c : input) {
if (c == ' ') {
words.push_back(word);
word = "";
} else {
}
}
er
word += c;
rn
words.push_back(word);
sort(words.begin(), words.end());
ea
return 0;
od
Java Code:
import java.util.Arrays;
Cc
Python Code:
m
def is_vowel(c):
return c in 'aeiouAEIOU'
.co
input_str = "apple banana cherry orange mango"
words = input_str.split()
words.sort()
er
for i, word in enumerate(words, 1):
if is_vowel(word[0]):
print(f"{i}: {word}")
rn
Output:
ea
1: apple
2: orange
el
After JEE Mains, students have been admitted to an engineering college, forming a class of 'n'
students. The Head of the Department (HOD) is tasked with selecting a class monitor. However,
Cc
the HOD meets the students one by one and has a peculiar way of choosing. Each time the
HOD meets a student, if the student's rank is lower than that of the previous student met, the
HOD cuts the previous student's name and writes the new student's name along with their rank
in a register. Given the ranks the HOD receives each time they meet a student, predict how
many names are cut from the list.
Constraints:
Output Format:
Single N space separated integers denoting the final situation with the array v.
m
Answer:
.co
Sample Input:
7
5863942
C++ Code:
er
#include <iostream>
rn
using namespace std;
int main() {
ea
int N;
cin >> N;
return 0;
}
Java Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
m
cuts++;
}
prevRank = currRank;
}
.co
System.out.println(cuts);
}
}
er
Python Code:
N = int(input())
rn
ranks = list(map(int, input().split()))
cuts = 0
prev_rank = ranks[0]
ea
prev_rank = rank
print(cuts)
od
Output: 3
Cc
Rahul is known for copying in exams from his adjacent students, but he's smart about it. Instead
of directly copying the words, he changes the positions of letters while keeping the letters
constant. As the examiner, you need to determine if Rahul has copied a certain word from the
adjacent student who is giving the same exam. You should provide Rahul with the appropriate
markings based on your findings.
Output Format:
m
Constraints:
.co
Answer:
Sample Input:
HELLO
EHLLO
er
rn
C++ Code:
#include <iostream>
ea
#include <algorithm>
using namespace std;
int main() {
el
sort(adjacentWord.begin(), adjacentWord.end());
sort(rahulWord.begin(), rahulWord.end());
if (adjacentWord == rahulWord) {
cout << 1 << endl;
Cc
} else {
cout << 0 << endl;
}
return 0;
}
Java Code:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String adjacentWord = sc.next();
String rahulWord = sc.next();
Arrays.sort(adjacentArray);
m
Arrays.sort(rahulArray);
if (Arrays.equals(adjacentArray, rahulArray)) {
System.out.println(1);
.co
} else {
System.out.println(0);
}
}
}
er
Python Code:
rn
adjacent_word = input()
rahul_word = input()
if sorted(adjacent_word.lower()) == sorted(rahul_word.lower()):
ea
print(1)
else:
print(0)
el
Output: 1
od
using stars (*) and dots (.) as shown below. Since Anirudh is not proficient in astronomy, can
you help him?
Answer:
Sample Input:
N=5
C++ Code:
#include <iostream>
using namespace std;
int main() {
int N;
cin >> N;
m
if (j < N - i || j >= N + i + 1) {
cout << ".";
} else {
cout << "*";
.co
}
}
cout << endl;
}
er
return 0;
rn
Java Code:
import java.util.Scanner;
ea
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
el
System.out.print(".");
} else {
System.out.print("*");
}
Cc
}
System.out.println();
}
}
}
Python Code:
N = int(input())
for i in range(N):
for j in range(N * 2):
if j < N - i or j >= N + i + 1:
print(".", end="")
else:
print("*", end="")
print()
Output:
**..**
m
*....*
......
*....*
.co
**..**
er
You are given an array, and you need to choose a contiguous subarray of length 'k'. Then, find
the minimum value within that subarray and return the maximum of those minimum values.
rn
Answer:
Sample Input:
ea
Explanation:
od
The subarrays of size 2 are: [3, 1], [1, 4], [4, 6], [6, 2], and [2, 5].
C++ Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int k, n;
cin >> k >> n;
vector<int> arr(n);
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
m
cout << max_min << endl;
return 0;
.co
}
Java Code:
import java.util.Scanner;
er
public class Main {
public static void main(String[] args) {
rn
Scanner sc = new Scanner(System.in);
int k = sc.nextInt();
int n = sc.nextInt();
int[] arr = new int[n];
ea
System.out.println(maxMin);
}
}
Python Code:
k, n = map(int, input().split())
arr = list(map(int, input().split()))
max_min = float('-inf')
for i in range(n - k + 1):
min_val = min(arr[i:i + k])
max_min = max(max_min, min_val)
print(max_min)
Output: 4
m
A password manager wants to create new passwords using two strings given by the user, then
combines them to create a harder-to-guess combination. Given two strings, the task is to
.co
interleave the characters of the strings to create a new string. Begin with an empty string and
alternately append a character from string 'a' and from string 'b'. If one of the strings is
exhausted before the other, append the remaining letters from the other string all at once. The
resulting string is the new password.
Example:
er
If a = 'hackerrank' and b = 'mountain', the resulting password is 'hmaocuknetrariannk'.
rn
Function Description:
Complete the function newPassword which takes two strings 'a' and 'b' as input and returns the
new password as a string.
ea
Parameters:
Returns:
od
Answer:
Cc
Sample Input:
a = "open"
b = "source"
C++ Code:
#include <iostream>
#include <string>
using namespace std;
m
while (i < a.length()) {
result += a[i++];
}
.co
while (j < b.length()) {
result += b[j++];
}
}
er
return result;
rn
int main() {
string a, b;
cin >> a >> b;
cout << newPassword(a, b) << endl;
ea
return 0;
}
Java Code:
el
import java.util.Scanner;
return result.toString();
}
m
}
Python Code:
.co
def newPassword(a, b):
result = ""
i, j = 0, 0
er
result += a[i] + b[j]
i += 1
j += 1
rn
while i < len(a):
result += a[i]
i += 1
ea
return result
a = input()
od
b = input()
print(newPassword(a, b))
Output: ‘ospuoerce’
Cc
Lean More: Top Infosys Coding Questions with Answers For Specialist Programmer
Input Format:
Two lines containing two strings representing the names (all letters are in capital case).
Output Format:
A single line containing the lexicographically smallest possible longest common subsequence.
m
Answer:
.co
Sample Input:
GEEK
EKEG
C++ Code:
er
#include <iostream>
rn
#include <vector>
#include <algorithm>
using namespace std;
ea
int main() {
string s1, s2;
cin >> s1 >> s2;
el
return 0;
m
}
Java Code:
.co
import java.util.Scanner;
er
String s1 = sc.next();
String s2 = sc.next();
rn
int[][] dp = new int[s1.length() + 1][s2.length() + 1];
}
}
}
od
System.out.println(lcs.toString());
}
}
Python Code:
m
if s1[i - 1] == s2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
.co
lcs = ""
i, j = m, n
while i > 0 and j > 0:
if s1[i - 1] == s2[j - 1]:
return lcs
s1 = input()
el
s2 = input()
print(lcs(s1, s2))
od
Output: ‘EG’