Code Questers Problems
Code Questers Problems
(20 points)
Problem statement:
A slot machine has multiple wheels that are spun n times. In each spin, each wheel may have multiple stops from 1
to 9 and shows one random number on the machine's dashboard.
Given the number of spins n, determine the minimum number of stops on each wheel to produce the numbers
displayed on the dashboard for each spin. Then, calculate the total stops.
Example
n=4
spins[] = [ '712', '246', '365', '312' ]
the spins on a slot machine with 3 wheels are recorded as an array, history:
712
246
365
312
One wheel needs to have at least 7 stops to produce the numbers displayed on the dashboard for 1st spin. Since 7 is
the highest value in any row, remove the highest value from each of the rows:
12
24
35
12
Now the highest value is 5, so another wheel must have 5 stops to produce the numbers displayed on the dashboard
for 3rd spin.
1
2
3
1
Using the same logic, the final wheel needs 3 stops. Total stops are 7 + 5 + 3 = 15.
Sample run 0:
4 Legend:
137 user input
364
115 output
724
14
Sample run 1:
4 Legend:
1112 user input
1111
1211 output
1111
Explanation
For the explanation, please refer to the problem statement.
Problem 2: Are they Pangrams?
(10 points)
Problem statement:
A string is a pangram if it contains all letters of the English alphabet, ascii['a'-'z']. Given a list of strings, determine if
each one is a pangram or not. Return "1" if true and "0" if false.
Example
pangram = ['pack my box with five dozen liquor jugs', 'this is not a pangram']
The string 'pack my box with five dozen liquor jugs' is a pangram , because it contains all the letters 'a' through 'z'.
Therefore, the result is true (1).
The string 'this is not a pangram' is not a pangram. Therefore, the result is false (0).
Assemble a string of the two results, in order. The result is '10'.
Constraints
1 ≤ n ≤ 10
Each string pangram[i] (where 0 ≤ i < n) is composed of lowercase letters and spaces.
1 ≤ length of pangram[i] ≤ 10^3
Sample run 0:
4 Legend:
we promptly judged antique ivory buckles for the next prize user input
we promptly judged antique ivory buckles for the prizes
the quick brown fox jumps over the lazy dog output
the quick brown fox jump over the lazy dog
1010
Sample run 1:
4 Legend:
cfchcfcvpalpqxenhbytcwazpxtthjumliiobcznbefnofyjfsrwfecxcbmoafes tnulqkvx user input
oxhctvhybtikkgeptqulzukfmmavacshugpouxoliggcomykdnfayayqutgwivwldrkp
gpecfrak zzaxrigltstcrdyhelhz rasrzibduaq cnpuommogatqem output
hbybsegucruhxkebrvmrmwhweirx mbkluwhfapjtga liiylfphmzkq
0000
Hint 1
How can you keep track of when a character has been seen?
Answer: Have a seen array of 26 integers initialized to zero. As a character is seen, update the array at the index
order(character) 1. Ignore spaces.
Hint 2
How will you know when all letters have been seen?
Answer: Each time you update the seen array, increment a counter.
Problem 3: Arrange the Words
(10 points)
Problem statement:
A sentence is defined as a string of space-separated words that starts with a capital letter followed by lowercase letters
and spaces and ends with a period. That is, it satisfies the regular expression ^[A-Z][a-z ]*\.$. Rearrange the words in a
sentence while respecting the following conditions:
Example
Order the sentence by word's length and keep the original order for the words with the same length.
Length 3: {and}
Length 4: {Cats, hats}
Reassemble the sequence of words so that the first letter is uppercase, the intermediate letters are lowercase, and the last
one is a period.
Sample run 0:
Legend:
The lines are printed in reverse order. user input
Sample run 1:
Legend:
Here i come. user input
A number of bids are being taken for a project. Determine the number of distinct pairs of project costs where their
absolute difference is some target value. Two pairs are distinct if they differ in at least one value.
Example
n=3
projectCosts = [1, 3, 5]
target= 2
There are 2 pairs [1,3], [3,5] that have the target difference target = 2, therefore a value of 2 is returned.
Constraints
5 ≤ n ≤ 10
0 < projectCosts[i] ≤ 2 × 10^2
Each projectCosts[i] is distinct, i.e. unique within projectCosts
1 ≤ target ≤ 10^2
Sample run 0:
5 Legend:
1 user input
5
3 output
4
2
2
Explanation
size of the array is 5.
projectCosts[5] = [1,5,3,4,2]
target is 2.
Count the number of pairs in projectCosts whose difference is target = 2. The following three pairs meet the
criterion: (1, 3), (5, 3), and (4, 2).
Sample run 0:
6 Legend:
2 user input
4
6 output
8
10
12
2
5
Problem 5: Lifting Weights
(20 points)
Problem statement:
An athlete is lifting weights. The maximum capacity of the barbell is maxCapacity. Each barbell plate has a weight,
weight[i]. Determine the maximum weight of plates that can be added to the barbell without exceeding
maxCapacity.
Example
weights = [7, 1, 5, 6, 2]
maxCapacity = 7
There are 3 ways to reach the maximum weight: {7}, {1, 6} and {2, 5}. Other combinations are either more or less
than 7 combined weight. Return 7.
Constraints
1 ≤ n ≤ 10
1 ≤ maxCapacity ≤ 10^2
1 ≤ weights[i][ ]≤ 10^2
Sample run 0:
3 Legend:
1 user input
3
5 output
7
Explanation
Sample run 1:
4 Legend:
4 user input
8
5 output
9
20
18
Problem 6: Connected Sum
(30 points)
Problem statement:
Given a number of nodes and a list of connected pairs, determine the weights of each isolated set of nodes assuming
each node weighs 1 unit. Then for each weight calculated, sum the ceiling of its square root and return the final sum.
Example
graph_nodes = 10
graph_from = [1, 1, 2, 3, 7]
graph_to = [2, 3, 4, 5, 8]
There are graph_edges = 5 edges to consider. There are 2 isolated sets with more than one node, {1, 2, 3, 4, 5} and {7,
8}. The ceilings of their square roots are 51/2 ≅ 2.236 and ceil(2.236) = 3, 21/2 ≅ 1.414 and ceil(1.414) = 2. The
other three isolated nodes are separate and the square root of their weights is 11/2 = 1 respectively.
The sum is 3 + 2 + (3 * 1) = 8.
Sample run 0:
8 4 Legend:
8 1 user input
5 8
7 3 output
8 6
Explanation
8 4 → graph_nodes = 8 nodes, graph_edges = 4 edges
8 1 → graph_from[] = [8, 5, 7, 8], graph_to[] = [1, 8, 3, 6]
The diagram below shows the groups of nodes: