2023 IMC QT OA

Number of Unique Sequences with Constraints

There are 5 numbers, and can be used repeatedly to form a length 5 sequence. Two constraints:

  1. No number can be used more than twice
  2. No same number can be adjacent.
from itertools import product

# Define the numbers
numbers = [0, 1, 2, 3, 4]

# Generate all possible permutations of length 5
all_sequences = list(product(numbers, repeat=5))

def is_valid(sequence):
    # Check if any adjacent numbers are the same
    for i in range(1, len(sequence)):
        if sequence[i] == sequence[i-1]:
            return False

    # Check if any number is used more than twice
    for num in numbers:
        if sequence.count(num) > 2:
            return False

    return True

# Filter out invalid sequences
valid_sequences = [seq for seq in all_sequences if is_valid(seq)]

# Print the number of valid sequences
print(len(valid_sequences))

# Uncomment the following line if you want to see all valid sequences:
# print(valid_sequences)
  1. The first number can be chosen freely, which has 5.
  2. The second number should be one of the rest 4.
  3. The third number is where things needed to be discussed
    • If the third number is the same as the first. The rest two positions would have 4 * 3 possibilities.
    • If the third number is different from the first, (and the second as well). It has 3 choices. Then the rest two positions can be chosen freely, 4 * 4.

In total we have 5 * 4 * (4 * 3 + 3 * 4 * 4) = 1200.

Assign Task for People with Different Skills

There are ten employees who must be assigned to various tasks. Three members will be working on an outreach project, four others will be focusing on product development, and the rest will focus on doing novel research.

Before you make the groups, two employees come forward and admit that they have no skill in research and should not be working on that team. Given this restriction, how many ways can you organize the teams?

The below simulation is fairly straightforward. Using math, we choose 3 people from 8 to do the research problem (excluding the two who are not capable). Then the rest 7 people can be divided into two groups, 4 and 3, to do the other two tasks. So we have 8 choose 3 * 7 choose 4 = 1960.

from itertools import combinations

# Define the 10 people
people = ["P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10"]

# People who can't do Task C
no_task_c = ["P9", "P10"]

# Count the valid assignments
valid_assignments = 0

# Iterate over all combinations of 3 people for Task A
for task_a in combinations(people, 3):
    remaining_after_a = [p for p in people if p not in task_a]
    
    # Iterate over all combinations of 4 people for Task B from the remaining people
    for task_b in combinations(remaining_after_a, 4):
        task_c = [p for p in remaining_after_a if p not in task_b]
        
        # Check if any of the people in Task C can't actually do Task C
        if not any(p in no_task_c for p in task_c):
            valid_assignments += 1

print(valid_assignments)
Previous
Next