0% found this document useful (0 votes)
19 views

Deloitte Coding Sheet

The document outlines various coding problems, each with a specific task and constraints. Problems include finding numbers with even digits, making anagrams, decoding nested strings, counting binary substrings, and more. Each problem provides sample inputs and outputs to illustrate the expected results.

Uploaded by

Harshitha botta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Deloitte Coding Sheet

The document outlines various coding problems, each with a specific task and constraints. Problems include finding numbers with even digits, making anagrams, decoding nested strings, counting binary substrings, and more. Each problem provides sample inputs and outputs to illustrate the expected results.

Uploaded by

Harshitha botta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Deloitte Coding Sheet

1. Find Numbers with Even Digits

Problem Statement:
In a distant kingdom, a wise king has a chest full of magical numbers. He decides to reward the
most observant mathematician in the land. Your task is to analyze a list of these numbers and
identify which ones contain an even number of digits. The king believes that only numbers with
an even number of digits hold the key to unlock the hidden treasures of the kingdom. Can you
help identify these numbers for the king?

Constraints:

● The array length 1≤n≤10^4


● Each number in the array is a positive integer 1≤num≤10^9

Sample Input:
123,4567,89,1001,22

Sample Output:
4567, 89, 1001, 22

Explanation:
Numbers like 123 have 3 digits, which is odd, so they are excluded. However, 89 and 22 have 2
digits, which is even, so they are included.

2. Making Anagrams

Problem Statement:
Alice is attending a cryptography class and has discovered that anagrams can be very useful.
She is working on an encryption scheme that involves two large strings. The encryption is
dependent on determining the minimum number of character deletions required to turn the
two strings into anagrams.
Two strings are considered anagrams if the letters of one string can be rearranged to form the
other string, meaning both strings must contain the same exact letters in the same exact
frequencies.

Given two strings, your task is to calculate the minimum number of deletions required to make
them anagrams. Characters can be deleted from either string.

Constraints:

● Both strings consist only of lowercase English letters (a to z).


● The lengths of the strings can vary but will not exceed ten to the power of four.

Input Format:

● The first line contains the first string.


● The second line contains the second string.

Output Format:

● Print a single integer denoting the minimum number of character deletions required to
make the strings anagrams.

Sample Input:

cde

abc

Sample Output:

Explanation:
To make the strings anagrams:

● Remove d and e from "cde" to get "c".


● Remove a and b from "abc" to get "c".

This results in a total of 4 deletions.


3. Decode Nested Encoded Strings

Problem Statement:
A mysterious letter arrives at your doorstep containing a cryptic message. The letter reads:
"Decode the message to reveal the treasure." The message is encoded using nested repetition,
such as "2[a3[b]]", where numbers indicate the number of repetitions. Your task is to decode
such messages. For instance, "2[a3[b]]" expands to "abbbabbb". Can you solve this puzzle and
unveil the treasure?

Constraints:

● The encoded string contains only alphanumeric characters, square brackets, and digits.
● The maximum nesting level is 100.
● The length of the encoded string does not exceed 10^4.

Sample Input:
"3[a2[bc]]"

Sample Output:
"abcbcabcbcabcbc"

Explanation:
The innermost part "2[bc]" expands to "bcbc". Then, "3[a2[bc]]" expands to "abcbcabcbcabcbc".

4. Count Binary Substrings with Equal 0s and 1s

Problem Statement:
A famous mathematician has been studying binary sequences and discovered a fascinating
pattern. He realized that there are certain substrings where the number of 0s is equal to the
number of 1s. Intrigued by this observation, he asks you to help find how many such substrings
exist in a given binary string. The mathematician believes that these balanced substrings can
lead to new discoveries in the world of numbers. Can you find them?

Constraints:

● The length of the binary string is 1≤n≤10^5


● The string only contains characters '0' and '1'.

Sample Input:
"00110011"

Sample Output:
6
Explanation:
The six balanced substrings are: "0011", "01", "1100", "10", "0011", and "01".

5. Calculate Salutes Exchanged by Soldiers

Problem Statement:
In a grand military parade, soldiers march in a straight line. Some soldiers are facing left, while
others are facing right. Every time a soldier facing right comes across a soldier facing left, they
exchange a salute. Your task is to calculate how many salutes happen in total. Can you
determine the total number of salutes based on the arrangement of soldiers in the parade?

Constraints:

● The length of the string is 1≤n≤1000


● The string consists of '>' and '<' characters only, representing soldiers facing right and
left, respectively.

Sample Input:
"><<<>"

Sample Output:
2

Explanation:
There are two salutes: one between the first '>' and the first '<', and the second between the
third '>' and the third '<'.

6. Sort Words Based on Embedded Numbers

Problem Statement:
A peculiar list of words has been found, where each word contains an embedded number
indicating its position in the correct order. Your task is to rearrange the words in the correct order
based on these embedded numbers. For example, "is2" and "Th1is" should be placed in the
correct sequence based on their numbers. Can you sort the list and reveal the correct sequence
of words?

Constraints:

● The length of the list is 1≤n≤1000


● Each word contains exactly one digit indicating its position (1-9).
Sample Input:
["is2", "a3", "Th1is"]

Sample Output:
This is a

Explanation:
The word "This" contains 1, so it comes first, followed by "is" and then "a".

7. Construct Concentric Squares of Numbers

Problem Statement:
In a grand palace, the floor is decorated with concentric squares made of numbers. The
outermost square has the largest number, and each subsequent inner square has a smaller
number. Your task is to construct these concentric squares based on a given size n. The size of
each square decreases as you move towards the center. Can you create the beautiful design of
concentric squares for the palace?

Constraints:

● The size of the square is n such that 1≤n≤50


● The number of squares is equal to n.

Sample Input:
3

Sample Output:

Explanation:
The outer square has the number 3, the next one has 2, and the innermost square has 1.

8. Return a String with Repeated Words Based on Embedded Counts


Problem Statement:
You have received a list of words, each with a number embedded indicating how many times
that word should be repeated. Your task is to construct a new string by repeating each word the
specified number of times. For example, the word "hello3" should be repeated 3 times. Can you
generate the final string based on this pattern?

Constraints:

● The number of words is 1≤n≤100.


● Each word contains exactly one number indicating how many times it should be
repeated.

Sample Input:
["hello3", "world2"]

Sample Output:
"hellohellohello worldworld"

Explanation:
"hello3" repeats "hello" 3 times, and "world2" repeats "world" 2 times.

9. Find All Happy Numbers in a Range

Problem Statement:
In a small village, people celebrate their happiness by performing a ritual with numbers. A
number is considered "happy" if the process of repeatedly summing the squares of its digits
eventually leads to 1. For example, the number 19 is happy because 1^2+ 9^2 = 82, 8^2 + 2^2=
68, and so on, eventually reaching 1. Your task is to find all happy numbers in a given range.
Can you help the villagers find their happy numbers?

Constraints:

● The range is between 1 and 10^4.

Sample Input:
1, 20

Sample Output:
[1, 7, 10, 13, 19]

Explanation:
The happy numbers between 1 and 20 are 1, 7, 10, 13, and 19.
10. Evaluate Product of Array Excluding the Current Index

Problem Statement:
In a world where magical creatures live in harmony, every creature has a unique power level
represented by an array. However, to determine the total power, each creature wants to know
the total power of all others except itself. Can you calculate the product of all numbers in the
array except the current one for each index, without using division? The creatures will be very
grateful if you can solve this for them.

Constraints:

● The length of the array is 1≤n≤10^5.


● Each number in the array is between 1≤num≤ 10^4.

Sample Input:
[1, 2, 3, 4]

Sample Output:
[24, 12, 8, 6]

Explanation:
For the first index, the product of all other numbers is 2×3×4=242 \times 3 \times 4 = 24.
Similarly, for the second index, it's 1×3×4=121 \times 3 \times 4 = 12, and so on.

11. Pyramid Pattern Using Numbers

Problem Statement:
A skilled artist is designing a pyramid-like pattern using numbers for a grand festival. The
pyramid consists of numbers arranged in increasing order from the top to the base, forming
rows. Each row in the pyramid contains a sequence of numbers that increases from 1 up to the
row number. The artist needs your help to create this pyramid pattern for any given number of
rows. Can you design the pyramid pattern for the festival?

Constraints:

● The number of rows n is between 1≤n≤1001

Sample Input:
4
Sample Output:

1
1 2
1 2 3
1 2 3 4

Explanation:
For 4 rows, the pyramid starts with "1" at the top, then "1 2" on the next row, and so on,
increasing the number sequence with each row.

12. Check if Two Strings are Permutations

Problem Statement:
Two friends, Alice and Bob, were given two strings and challenged to check if one is a
permutation of the other. Alice insists that a permutation should contain the same characters but
in a different order. Bob, on the other hand, is skeptical. Can you help them by determining if
one string is a permutation of the other?

Constraints:

● The length of both strings is 1≤n≤10^5.


● The strings consist of uppercase and lowercase English alphabets.

Sample Input:
"abc", "cab"

Sample Output:
True

Explanation:
Both strings contain the same characters in different orders, so they are permutations of each
other.

13. Determine the Score of Balanced Parentheses

Problem Statement:
In a world of balanced equations, mathematicians have designed a set of parentheses rules.
Each pair of balanced parentheses has a score based on its depth, where deeper pairs have
higher scores. Your task is to calculate the total score of the balanced parentheses string, where
each pair of parentheses has a score equal to the sum of its score at that depth. Can you help
them evaluate the score of the parentheses?

Constraints:

● The length of the parentheses string is 1≤n≤1000.


● The string only contains '(' and ')'.

Sample Input:
"(()())"

Sample Output:
6

Explanation:
The score of the string is calculated as follows: the outermost pair contributes 2, and the inner
pair contributes 4, making the total score 6.

14. Arrange Seats in a Circular Order, Maintaining Gaps

Problem Statement:
At a royal banquet, there are a number of seats arranged in a circular fashion. The guests must
be seated such that there is a specific gap between each of them. Your task is to arrange the
guests in these seats while maintaining the required gaps. Can you solve the seating
arrangement puzzle?

Constraints:

● The number of guests n is between 1≤n≤1000.


● Each guest must sit in a unique seat.

Sample Input:
4, 2

Sample Output:
[1, 3, 4, 2]

Explanation:
The guests are seated in positions 1, 3, 4, and 2 to maintain a gap of 2 between each guest.

15. Create a Lexicographically Smallest Palindrome


Problem Statement:
In a kingdom of words, the royal council has tasked you with creating the lexicographically
smallest palindrome from a given string. A palindrome is a word that reads the same forward
and backward. The council wants the smallest lexicographical palindrome that can be formed by
modifying the given string. Can you help them achieve this?

Constraints:

● The length of the string is 1≤n≤100.

Sample Input:
"abca"

Sample Output:
"abba"

Explanation:
The smallest palindrome that can be formed is "abba".

16. Decode and Evaluate a Nested List of Operations

Problem Statement:
A wizard has encoded a series of operations into a nested list format, and you must decode it to
evaluate the result. Each operation may involve simple arithmetic or deeper nested operations.
Your task is to evaluate the expression after decoding the nested list. Can you help the wizard
solve the encoded equation?

Constraints:

● The number of operations nnn is between 1≤n≤100


● The operations involve basic arithmetic operations (+, -, *, /).

Sample Input:
["1", "+", ["2", "*", "3"]]

Sample Output:
7

Explanation:
The decoded operation is 1+(2∗3)=7
17. Calculate the Number of Subsequences in an Array that Meet a
Condition

Problem Statement:
In a kingdom of arrays, there is a rule that only certain subsequences can be accepted for the
royal collection. Your task is to calculate how many subsequences in a given array meet a
specific condition. The condition involves checking if the subsequence satisfies certain
arithmetic properties. Can you calculate the number of such subsequences?

Constraints:

● The length of the array is 1≤n≤1000

Sample Input:
[1, 2, 3]

Sample Output:
6

Explanation:
There are 6 subsequences: [1], [2], [3], [1, 2], [1, 3], [2, 3].

18. Identify the First Occurrence of One Word in Another

Problem Statement:
In a library of books, you are tasked with identifying the first occurrence of one word within
another. The librarian has given you two words, and you must determine where the second
word first appears in the first word. Can you find the position of the first occurrence efficiently?

Constraints:

● The length of both words is 1≤n≤1000

Sample Input:
"hello", "ll"

Sample Output:
2

Explanation:
The word "ll" first appears at index 2 in "hello".
19. Calculate the Total XOR of All Subsets of an Array

Problem Statement:
A group of mathematicians has been studying the properties of the XOR operation, and they are
particularly interested in the XOR of all possible subsets of an array. Your task is to calculate the
total XOR of all subsets. Can you help them with this complex mathematical task?

Constraints:

● The length of the array is 1≤n≤10^5


● Each element in the array is between 1≤a[i]≤1000

Sample Input:
[1, 2, 3]

Sample Output:
0

Explanation:
The total XOR of all subsets is 0.

20. Count All Subarrays that Sum Up to a Given Value

Problem Statement:
In a kingdom of sums, there is a challenge where you must find all subarrays whose sum equals
a given value. The kingdom's ruler has given you an array, and you must determine how many
subarrays meet this condition. Can you solve the sum puzzle?

Constraints:

● The length of the array is 1≤n≤1000


● The sum value is an integer between −1000≤k≤1000

Sample Input:
[1, 2, 3], 3

Sample Output:
2

Explanation:
There are 2 subarrays that sum up to 3: [1, 2] and [3].
21. Find Numbers in an Array with an Even Number of Digits

Problem Statement:
A mathematician in the kingdom is fascinated by the number of digits in numbers. He wonders
how many numbers in an array have an even number of digits. Your task is to find how many
numbers in the given array have an even number of digits. Can you solve the puzzle?

Constraints:

● The length of the array is 1≤n≤1000


● The numbers in the array range from 1≤num≤10^5

Sample Input:
[12, 345, 2, 6, 7896]

Sample Output:
2

Explanation:
The numbers 12 and 7896 have an even number of digits.

22. Group Anagrams in a List of Strings

Problem Statement:
A kingdom has many different types of spells, represented as words. Each spell can be cast in
multiple ways, with the same letters rearranged. Your task is to group the spells (strings) that
are anagrams of each other. Can you group the anagrams and display them together?

Constraints:

● The number of words nnn is between 1≤n≤10001 \leq n \leq 10001≤n≤1000.

Sample Input:
["eat", "tea", "tan", "ate", "nat", "bat"]

Sample Output:
[["eat", "tea", "ate"], ["tan", "nat"], ["bat"]]

Explanation:
"eat", "tea", and "ate" are anagrams of each other, and "tan" and "nat" are another group.

23. Stick Cutting Problem


You are given a collection of sticks of varying lengths. Your task is to repeatedly cut the sticks as
follows:

1. Determine the shortest stick length in the collection.


2. Cut all sticks by that shortest length.
3. Discard all sticks that become zero after the cut.
4. Count and record the number of sticks remaining before each cut.

Continue the process until there are no sticks left.

Input:

● An integer nnn (1 ≤ n ≤ 10^3), the number of sticks.


● An array arr of nnn positive integers (1 ≤ arr[i] ≤ 10^6), representing the lengths of the
sticks.

Output:

● A list of integers, where each integer indicates the number of sticks remaining before
each cut.

Example

Input:

n = 6

arr = [5, 4, 4, 2, 2, 8]

Output:

[6, 4, 2, 1]

Explanation:

● Initial stick lengths: [5, 4, 4, 2, 2, 8]


○ Shortest stick is 2, so cut all sticks by 2. Remaining lengths: [3, 2, 2, 6]
○ Number of sticks: 6
● Remaining lengths: [3, 2, 2, 6]
○ Shortest stick is 2, so cut all sticks by 2. Remaining lengths: [1, 4]
○ Number of sticks: 4
● Remaining lengths: [1, 4]
○ Shortest stick is 1, so cut all sticks by 1. Remaining lengths: [3]
○ Number of sticks: 2
● Remaining lengths: [3]
○ Shortest stick is 3, so cut all sticks by 3. Remaining lengths: []
○ Number of sticks: 1

The process ends when no sticks remain.

24. Alice and Bob's Challenge Ratings

Alice and Bob each created a challenge. These challenges are scored based on three
categories: clarity, originality, and difficulty. The scores for Alice's challenge are given as a =
[a[0], a[1], a[2]], and the scores for Bob's challenge are given as b = [b[0], b[1],
b[2]].

Task:

Compare their scores category by category:

1. If a[i] > b[i], Alice earns 1 point.


2. If a[i] < b[i], Bob earns 1 point.
3. If a[i] == b[i], no points are awarded.

You need to calculate the total points for Alice and Bob and return them as [Alice's score,
Bob's score].

Input:

● a: A list of 3 integers representing Alice's scores.


● b: A list of 3 integers representing Bob's scores.

Output:

● A list [Alice's score, Bob's score].

Example:

Input:

1. a = [5, 6, 7]
2. b = [3, 6, 10]

Output:

[1, 1]
Explanation:

● Compare a[0] = 5 with b[0] = 3: Alice earns 1 point.


● Compare a[1] = 6 with b[1] = 6: No points are awarded.
● Compare a[2] = 7 with b[2] = 10: Bob earns 1 point.
● Final result: [1, 1].

Constraints:

1. 1 ≤ a[i], b[i] ≤ 100 for all i.


2. Each list contains exactly 3 integers.

25. Longest Palindromic Substring

Problem Statement:
In a kingdom of words, the royal scholars have tasked you with finding the longest palindromic
substring from a given string. A palindrome is a string that reads the same forwards and
backwards. The scholars want to know which substring in the string is the longest palindrome.
Can you help them solve this problem?

Constraints:

● The length of the string n is between 1≤n≤1000

Sample Input:
"babad"

Sample Output:
"bab" (or "aba")

Explanation:
The longest palindromic substring is "bab" or "aba". Both are valid answers.

26. Page-Turning Problem

A teacher asks the class to turn to a specific page number in a book. A student can start turning
pages either from the front of the book or the back. The task is to calculate the minimum
number of page turns required to reach the desired page.

Rules:

1. The book always starts with page 1 on the right side.


2. Pages are printed in pairs:
○ Page 1 on the right; page 2 on the left of the next page.
○ The last page may stand alone if the book has an odd number of pages.
3. Students can start turning pages from:
○ The front of the book (starting at page 1).
○ The back of the book (starting from the last page).

Examples:

Example 1:

Input:

n = 6

p = 2

Output:

Explanation:

● If the student starts from the front:


Page turns = 1 (from page 1 to page 2).
● If the student starts from the back:
Page turns = 2 (from page 6 to page 2).
● Minimum = 1.

Example 2:

Input:

n = 5

p = 4

Output:

Explanation:

● If the student starts from the front:


Page turns = 2 (from page 1 to page 4).
● If the student starts from the back:
Page turns = 0 (from page 5 to page 4).
● Minimum = 0.

27. Minimum Number of Platforms Required for a Railway Station

Problem Statement:
In a busy railway station, trains are scheduled to arrive and depart at specific times. You need to
determine the minimum number of platforms required to accommodate all the trains at the
station at any given time. Can you calculate this efficiently?

Constraints:

● The number of trains n is between 1≤n≤1000


● Train arrival and departure times are between 00:00 and 23:59.

Sample Input:
Arrivals: [100, 101, 300], Departures: [200, 400, 500]

Sample Output:
2

Explanation:
At time 100, the first train arrives. At time 200, the first train departs, and the second train
arrives. The station needs 2 platforms at the peak time.

28. Find All Divisors of a Number

Problem Statement:
A mathematician in the kingdom is studying the divisors of numbers. He wants to find all divisors
of a given number efficiently. Your task is to compute and return all divisors of a number. Can
you help him with this problem?

Constraints:

● The number n is between 1≤n≤10^6

Sample Input:
28

Sample Output:
[1, 2, 4, 7, 14, 28]
Explanation:
The divisors of 28 are 1, 2, 4, 7, 14, and 28.

29. Caesar Cipher Problem

Julius Caesar encrypted his messages using a cipher that shifts each letter of the alphabet by a
fixed number of positions. If the shift moves past the end of the alphabet, it wraps around to the
beginning.

Problem Description:

Given a string s, encrypt it using a Caesar cipher with a shift value k. Non-alphabetic characters
(such as -) remain unchanged.

Rules:

1. Only alphabetic characters (a-z and A-Z) are encrypted.


2. Non-alphabetic characters remain unchanged.
3. The shift value wraps around the alphabet:
○ Example: with k = 2, 'z' becomes 'b' and 'Z' becomes 'B'.
4. The case of the letters is preserved:
○ Lowercase remains lowercase, uppercase remains uppercase.

Constraints:

● 1≤length of s≤100
● 0≤k≤1000

Example Input/Output:

Input:

11

middle-Outz

Output:
okffng-Qwvb

Explanation:

Rotating the alphabet by 2:


Original alphabet: abcdefghijklmnopqrstuvwxyz

Rotated alphabet (+2): cdefghijklmnopqrstuvwxyzab

● Encrypting each letter:


○ m -> o
○ i -> k
○ d -> f
○ l -> n
○ e -> g
○ O -> Q
○ u -> w
○ t -> v
○ z -> b

30. Search a 2D Matrix

Problem Statement:
In a kingdom, there is a magical 2D matrix where each row is sorted in ascending order from left
to right, and the first element of each row is greater than the last element of the previous row.
Your task is to search for a target number in the matrix. Can you find it efficiently?

Constraints:

● The number of rows mmm is between 1≤m≤1000


● The number of columns nnn is between 1≤n≤1000.

Sample Input:
Matrix:
[
[1, 4, 7, 11],
[2, 5, 8, 12],
[3, 6, 9, 16],
[10, 13, 14, 17]
]
Target: 5

Sample Output:
True

Explanation:
The target number 5 exists in the matrix at position (1, 1), so the result is True.

Prime coders,
If you are done try to solve codechef <1000 rating questions. (Happy Coding)

Visit Prime Coding now: www.primecoding.in

You might also like