Deloitte Previous Coding Questions
π₯ Deloitte Coding Questions with Java & Python Solutions
Welcome to our coding practice series based on real Deloitte interview questions. In this post, weβve compiled 4 commonly asked problems that test your grasp of strings, permutations, recursion, and text handling. For each problem, weβve included:
- A clear problem statement
- Working Java and Python solutions
- A detailed explanation
Letβs dive into the problems and improve your problem-solving skills! πͺ
β Problem 1: Unique Substrings / Permutation Count
π Problem Statement: Unique Permutation Generator
Youβre building a password generator system. Given a string, generate all unique permutations of that string.
The input string may contain repeated characters. Your output should contain no duplicates and should be sorted in lexicographical order.
π’ Input:
A string s
(may contain repeated characters)
Example Input:
aba
π§Ύ Output:
All unique permutations printed line-by-line in lexicographical order.
Example Output:
aab
aba
baa
π£Code
from itertools import permutations s = input() unique = sorted(set([''.join(p) for p in permutations(s)])) for perm in unique: print(perm)
import java.util.*; public class UniquePermutations { static Set<String> result = new TreeSet<>(); public static void generate(String s, String ans) { if (s.length() == 0) { result.add(ans); return; } for (int i = 0; i < s.length(); i++) { generate(s.substring(0, i) + s.substring(i + 1), ans + s.charAt(i)); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String input = sc.next(); generate(input, ""); for (String perm : result) System.out.println(perm); } }
π Explanation:
We generate all possible permutations and eliminate duplicates using a set.
- In Java,
TreeSet
keeps elements sorted and unique. - In Python,
set
ensures uniqueness andsorted()
gives the desired order.
This approach works well even when characters repeat.
β Problem 2: String Rotation Check
π Problem Statement: Rotation Validator
Check whether one string is a rotation of another.
A rotation means taking characters from the start of a string and moving them to the end without changing their order.
π’ Input:
Two strings s1
and s2
Example Input:
abcde
deabc
π§Ύ Output:
Example Output:
Yes
Return "Yes"
if the second string is a valid rotation of the first, else "No"
.
π£Code
s1 = input() s2 = input() if len(s1) == len(s2) and s2 in (s1 + s1): print("Yes") else: print("No")
import java.util.Scanner; public class StringRotation { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1 = sc.next(); String s2 = sc.next(); if (s1.length() == s2.length() && (s1 + s1).contains(s2)) { System.out.println("Yes"); } else { System.out.println("No"); } } }
π Explanation:
We double the first string and check if the second is a substring.
This technique is effective for rotation validation because:
- All possible rotations of a string are present in its concatenation with itself.
β Problem 3: Sum of Digits Until Single Digit
π Problem Statement: Digital Root Calculation
You’re given a number. Your task is to repeatedly sum its digits until a single-digit result is obtained.
π’ Input:
A single integer num
Example Input: 987
π§Ύ Output:
A single-digit number
Example Output: 6
(Since 9 + 8 + 7 = 24 β 2 + 4 = 6)
π£Code
num = int(input()) while num >= 10: num = sum(int(d) for d in str(num)) print(num)
import java.util.Scanner; public class DigitalRoot { public static int digitalRoot(int num) { while (num >= 10) { int sum = 0; while (num > 0) { sum += num % 10; num /= 10; } num = sum; } return num; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.println(digitalRoot(n)); } }
π Explanation:
This process is often called the digital root.
The idea is to:
- Convert the number to digits
- Sum them
- Repeat until a single digit remains
Efficient and simple to implement in both languages.
β Problem 4: Special Character Removal & Word Count
π Problem Statement: Clean Text and Count Words
You’re given a messy sentence full of symbols. Remove all non-alphabetic characters (except space) and count the number of words.
π’ Input:
A string of messy text
Example Input: Hello! This is #100% amazing.
π§Ύ Output:
An integer indicating the number of valid words
Example Output: 5
π£Code
text = input() # Keep only letters and spaces cleaned = "" for char in text: if char.isalpha() or char == " ": cleaned += char # Split into words and count words = cleaned.strip().split() print(len(words))
import java.util.Scanner; public class CleanTextCount { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String text = sc.nextLine(); text = text.replaceAll("[^a-zA-Z ]", ""); String[] words = text.trim().split("\\s+"); System.out.println(words.length); } }
π Explanation:
We clean the text (using loop in Python and regex in Java):
Remove everything except alphabets and spaces
Split the cleaned sentence into words
Count them
This is useful in text preprocessing and word analysis tasks.
π¨ Deloitte Overview!
Want to know how Deloitte’s exam process works, what rounds to expect, and other important details? Check out this complete overview:
π https://nextgenkodinghub.in/deloitte-overview/
All the best! πΌ