1p3a
Question
·
Oct 2025
TekSystems Online Assessment Algorithm Interview Experience
Question Details
Problem Statement Goal: Minimize the number of "unhappy" students when assigning friend groups to a trip with a limited capacity. Input: 1. groups: An array of length $n$, where each eleme
Full Details
Problem Statement Goal: Minimize the number of "unhappy" students when assigning friend groups to a trip with a limited capacity. Input: 1. groups: An array of length $n$, where each element represents a student's group ID. Students sharing the same ID belong to the same friend group. 2. t: An integer representing the maximum capacity of the trip. Definitions: * Friend Group Size: The number of students sharing the same group ID. * Unhappy Student: A student is defined as unhappy if they are unable to go on the trip while other members of their friend group do go (i.e., the group is split). Students in groups where no one goes are not considered unhappy. Objective: Select a subset of students to fill the trip capacity $t$ such that the total number of unhappy students is minimized. Ideally, you want to fit friend groups perfectly to avoid splitting them. Examples: * Example 1: * groups = [1, 1, 2, 2, 2, 3, 3] (Group sizes: ${2, 3, 2}$) * t = 4 * Optimal Approach: Taking Group 1 (size 2) and Group 3 (size 2) fills the capacity exactly ($2+2=4$). No group is split. * Note: If a Greedy approach is used (taking largest group of 3 first), 1 slot remains. Splitting the next group of 2 leaves 1 person behind, resulting in 1 unhappy student. * Example 2: * groups = [1, 1, 2, 2, 2, 3, 3, 4] (Group sizes: ${2, 3, 2, 1}$) * t = 3 * Result: 0 unhappy students (Taking Group 2 of size 3 fills the capacity perfectly). --- # Efficient Algorithmic Solution The Greedy approach (sorting by frequency and assigning) fails because this is a variation of the Knapsack Problem or Subset Sum Problem. A greedy strategy does not guarantee that the combination of groups sums exactly to $t$. ### Recommended Approach: Dynamic Programming (Subset Sum) To solve this efficiently, treat the group sizes as items with weights. We want to find a subset of these weights that sums to exactly $t$, or as close to $t$ as possible, to minimize the split. Algorithm Steps: 1. Frequency Map: Convert the input array groups into a list of group sizes. * Input: [1, 1, 2, 2, 2, 3, 3] $\rightarrow$ Sizes: [2, 3, 2] 2. Boolean DP (Reachability): Use a boolean array dp of size $t + 1$. dp[i] will be true if a capacity of i can be filled perfectly using whole groups. * Initialize dp[0] = true, all others false. * Iterate through each group size s. * Update the DP table: for j from t down to s, dp[j] = dp[j] OR dp[j - s]. 3. Determine Minimum Unhappiness: * Case A (Perfect Fit): Check dp[t]. If true, a subset of groups sums exactly to t. No groups need to be split. Answer: 0. * Case B (Split Required): If dp[t] is false, we cannot fill the trip with whole groups. We must fill the remaining space by splitting one group. * Iterate through all reachable capacities i (where dp[i] is true). * For each valid i, the remaining space is rem = t - i. * We need to find a remaining group of size G > rem to fill this gap. The number of unhappy students will be G - rem (the portion of the group left behind). * Minimize G - rem across all valid combinations. Complexity: * Time Complexity: $O(N \times t)$, where $N$ is the number of groups. * Space Complexity: $O(t)$ for the DP array.
Free preview. Unlock all questions →
Topics
Arrays
Dynamic Programming
Sorting