Previous HackWithInfy Questions
Previous HackWithInfy Questions
The Questions from previous HackWithInfys play a crucial role in preparation for the examination. These
questions provide insight into the type and difficulty level of questions that can be expected in the
actual program.
Question 1
As the manager of the hotel, you have N guests to attend to, and each guest has a happiness value (Ci)
that depends on when they are served. The unhappiness of a guest is determined by the difference
between their happiness value (Ci) and the time (x) they are served, which is calculated as |Ci – x|. Your
goal is to find the minimum total unhappiness by serving all guests in any order you choose.
Please note that at a given time, only one guest can be served, and each guest takes exactly one unit of
time to be served.
1 <= Ci <= N
For example:
Input: 4 2 2 3 3
Output: 2
Input: 4 1 1 1 1
Output: 6
Question 2
You’re tasked with finding the total number of positive integer pairs (A, B) where A, B <= N, A^B <= X and
A+B is divisible by D. Given the inputs N, X and D, return the result modulo 10^9+7 as the total number
of pairs can be large.
Here’s an example: for the inputs 4, 3, 2, there are 6 pairs that meet the conditions: (1,1), (1,3), (2,2),
(3,1), (3,3), (4,4).
And for debugging purposes, a sample test case of 100, 121, 2 should return 4778.
Question 3
Given an array of size N and the limit M, find the maximum number of triplets that can be formed such
that each element of the array is <= M. A triplet is considered valid if it meets either of the following
criteria:
All numbers in the triplet are the same.
Constraints:
Example:
Input:
42
1222
Output:
Question 4
Find the minimum possible absolute difference between the maximum and minimum sum of elements
in 4 non-empty contiguous subsequences of an array A of N elements by making 3 cuts.
Constraints:
Input:
10
10 71 84 33 6 47 23 25 52 64
Output:
36
Question 5
You are given N people and K days, each day represented by an array of N numbers, indicating the order
in the people who arrived at the theatre. Your task is to find the largest group of people who arrive in
the same order for all K days.
Constraints:
1 <= K <= 10
Sample Input:
N=4, K=3
Day 1: [1, 3, 2, 4]
Day 2: [1, 3, 2, 4]
Day 3: [1, 4, 3, 2]
Sample Output:
Question 6
You are given a string of length n consisting of numbers and question marks (# represents a mine and
the number represents the sum of the number of mines in the adjacent cells). Your task is to fill the
question marks with either 0 or 1 in such a way that the string represents the correct sum of mines in
the adjacent cells. Return the number of ways to fill the question marks.
Constraints:
Example Input:
??0?1?
Example Output:
Example explanation: Two possible ways to fill the question marks are #1001# and 00001#.