DeloitteIT Codes Zone

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 and sorted() 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! πŸ’Ό


Thanks for visiting! Explore the categories below for more exciting and useful content.


 

Leave a Reply

Your email address will not be published. Required fields are marked *