Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the total sum is even.
DSA Typescript
const totalSum = nums.reduce((acc, num) => acc + num, 0); if (totalSum [1] 2 !== 0) { return false; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using division (/) instead of modulus (%) to check evenness.
Using multiplication (*) or addition (+) which do not check divisibility.
✗ Incorrect
We use the modulus operator % to check if the total sum is divisible by 2 (even).
2fill in blank
mediumComplete the code to initialize the target sum for subset search.
DSA Typescript
const target = totalSum [1] 2;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Multiplying instead of dividing.
Adding or subtracting which changes the target incorrectly.
✗ Incorrect
We divide totalSum by 2 to find the target sum for one subset.
3fill in blank
hardFix the error in the loop that updates the dp array for subset sums.
DSA Typescript
for (let num of nums) { for (let j = target; j >= [1]; j--) { dp[j] = dp[j] || dp[j - num]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting inner loop from 0 which causes incorrect dp updates.
Starting from target or 1 which does not prevent overwriting.
✗ Incorrect
We start from target down to num to avoid using updated dp values in the same iteration.
4fill in blank
hardFill both blanks to create a dp array and initialize the base case.
DSA Typescript
const dp: boolean[] = new Array([1] + 1).fill(false); dp[[2]] = true;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing dp size with nums.length instead of target.
Setting dp[1] = true instead of dp[0].
✗ Incorrect
We create dp array of size target+1 and set dp[0] = true as base case (sum 0 is always possible).
5fill in blank
hardFill all three blanks to complete the function that returns if partition is possible.
DSA Typescript
function canPartition(nums: number[]): boolean {
const totalSum = nums.reduce((acc, num) => acc + num, 0);
if (totalSum [1] 2 !== 0) return false;
const target = totalSum [2] 2;
const dp: boolean[] = new Array(target + 1).fill(false);
dp[0] = true;
for (const num of nums) {
for (let j = target; j >= num; j--) {
dp[j] = dp[j] || dp[j - num];
}
}
return dp[[3]];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators for checking evenness or division.
Returning dp[0] or dp[num] instead of dp[target].
✗ Incorrect
We check if totalSum % 2 !== 0, divide totalSum by 2 for target, and return dp[target].