Deloitte
Deloitte
Aptitude - https://www.indiabix.com/
HR - link
1. https://prepinsta.com/deloitte/coding-questions-and-answers/
2. https://ccodelearner.com/blog/deloitte-coding-questions-with-answers/
3. https://www.ccbp.in/blog/articles/deloitte-coding-questions-and-answers
4. https://qset.io/question-set/C9ONRck4N27b9ApekNLL?tab=answers
Answer:
Sample Input: 34
C++ Code:
#include <iostream>
using namespace std;
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;
}
int main() {
int num = 34;
findPrimeSum(num);
return 0;
}
Java Code:
public class PrimeSum {
Python Code:
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):
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."
# Sample input
n = 34
find_prime_sum(n)
Output:
Answer:
Sample Input: 28
C++ Code:
#include <iostream>
using namespace std;
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;
}
Java Code:
public class PerfectNumber {
Python Code:
def is_perfect(n):
sum = 1
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
sum += i
if i != n // i:
sum += n // i
return sum == n and n != 1
# 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.
Answer:
Sample Input:
6
10 20 30 40 50 60
3
C++ Code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
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;
Python Code:
N = int(input())
a = list(map(int, input().split()))
n = int(input())
result = " ".join(str(i >> n) for i in a)
print(result)
Output:
‘1 2 3 5 6 7’
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
positions in the sorted list.
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;
getline(cin, input);
vector<string> words;
string word = "";
for (char c : input) {
if (c == ' ') {
words.push_back(word);
word = "";
} else {
word += c;
}
}
words.push_back(word);
sort(words.begin(), words.end());
return 0;
}
Java Code:
import java.util.Arrays;
Python Code:
def is_vowel(c):
return c in 'aeiouAEIOU'
words = input_str.split()
words.sort()
Output:
1: apple
2: orange
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,
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.
Answer:
Sample Input:
7
5863942
C++ Code:
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();
System.out.println(cuts);
}
}
Python Code:
N = int(input())
ranks = list(map(int, input().split()))
cuts = 0
prev_rank = ranks[0]
print(cuts)
Output: 3
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:
Answer:
Sample Input:
HELLO
EHLLO
C++ Code:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string adjacentWord, rahulWord;
cin >> adjacentWord >> rahulWord;
sort(adjacentWord.begin(), adjacentWord.end());
sort(rahulWord.begin(), rahulWord.end());
if (adjacentWord == rahulWord) {
cout << 1 << endl;
} 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);
Arrays.sort(rahulArray);
if (Arrays.equals(adjacentArray, rahulArray)) {
System.out.println(1);
} else {
System.out.println(0);
}
}
}
Python Code:
adjacent_word = input()
rahul_word = input()
if sorted(adjacent_word.lower()) == sorted(rahul_word.lower()):
print(1)
else:
print(0)
Output: 1
Answer:
Sample Input:
N=5
C++ Code:
int main() {
int N;
cin >> N;
return 0;
}
Java Code:
import java.util.Scanner;
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:
**..**
*....*
......
*....*
**..**
Answer:
Sample Input:
Explanation:
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];
}
return 0;
}
Java Code:
import java.util.Scanner;
System.out.println(maxMin);
}
}
Python Code:
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
Example:
If a = 'hackerrank' and b = 'mountain', the resulting password is 'hmaocuknetrariannk'.
Function Description:
Complete the function newPassword which takes two strings 'a' and 'b' as input and returns the
new password as a string.
Parameters:
Returns:
Answer:
Sample Input:
a = "open"
b = "source"
C++ Code:
#include <iostream>
#include <string> using
namespace std;
return result;
}
Java Code:
import java.util.Scanner;
return result.toString();
}
Python Code:
return result
a = input()
b = input()
print(newPassword(a, b))
Output: ‘ospuoerce’
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.
Answer:
Sample Input:
GEEK
EKEG
C++ Code:
return 0;
}
Java Code:
import java.util.Scanner;
}
}
Python Code:
lcs = ""
i, j = m, n
while i > 0 and j > 0:
if s1[i - 1] == s2[j - 1]: lcs
= s1[i - 1] + lcs
i -= 1
j -= 1
elif dp[i - 1][j] > dp[i][j - 1]: i
-= 1
else:
j -= 1
return lcs
s1 = input()
s2 = input()
print(lcs(s1, s2))
Output: ‘EG’