Hacker Rank - Part 1
Number of duplicates
Write a TypeScript function that takes three arrays (names, prices, and weights) as input, where each index represents an item with its name, price, and weight. The function should determine the number of unique duplicate items based on the combination of name, price, and weight. For example, given the inputs:
const names = ['apple', 'mango', 'apple', 'banana', 'apple'];
const prices = [20, 20, 30, 30, 30];
const weights = [1, 3, 1, 3, 1];
The function should return 1 because the combination of name[2], price[2], weight[2] and name[4], price[4], weight[4] is a duplicate. Implement the function and explain how it works.
| Index | Name | Price | Weight |
|---|---|---|---|
| 0 | apple | 20 | 1 |
| 1 | mango | 20 | 3 |
| 2 | apple | 30 | 1 |
| 3 | banana | 30 | 3 |
| 4 | apple | 30 | 1 |
- My Idea
- Optimized Code
function findDuplicates(names: string[], prices: number[], weights: number[]): number {
const arr: string[] = [];
names.forEach((el, i) => {
prices.forEach((rs, j) => {
weights.forEach((kg, k) => {
if (i === j && i === k) {
arr.push(`${names[i]}-${prices[j]}-${weights[k]}`);
}
});
});
});
// the total time complexity of the above nested loops is: O(n³).
const dupl = arr.filter((value, i) => arr.indexOf(value) !== i);
// The `filter` method runs in O(n²) because for each element, it performs an `indexOf` operation
return dupl.length;
}
function findDuplicates(names: string[], prices: number[], weights: number[]): number {
const seen = new Set<string>(); // To track unique combinations
const duplicates = new Set<string>(); // To track duplicates
names.forEach((_, i) => {
// Create a unique key for each combination of name, price, and weight
const key = `${names[i]}-${prices[i]}-${weights[i]}`;
if (seen.has(key)) {
// If the key is already in the set, it's a duplicate
duplicates.add(key);
} else {
// Otherwise, add it to the seen set
seen.add(key);
}
});
return duplicates.size; // Return the number of unique duplicate combinations
}
Comparison with Original Code
| Feature | Original Code | Optimized Code |
|---|---|---|
| Time Complexity | O(n³ + n²) | O(n) |
| Space Complexity | O(n) (due to arr) | O(n) (due to Set) |
| Duplicate Detection | Uses filter and indexOf | Uses Set for constant-time checks |
| Readability | More complex with nested loops | Simplified with a single loop |
Minimum number of recommendation engines required
This problem is about determining the minimum number of recommendation engines required to process overlapping user sessions on an online streaming platform. Each session has a start and end time, and each session requires exactly one recommendation engine. The goal is to calculate how many engines are needed to handle all sessions without conflicts.
Problem Explanation
-
Input:
- A 2D array
sessionTimingsof sizen x 2, where each element represents[startTime, endTime]of a session. - Example:
const sessionTimings = [
[1, 4], // Session 1: Starts at time 1, ends at time 4
[2, 5], // Session 2: Starts at time 2, ends at time 5
[7, 9], // Session 3: Starts at time 7, ends at time 9
];
- A 2D array
-
Output:
- The minimum number of recommendation engines required to process all sessions.
- Example:
- For the above input, the output is
2because:- Session 1 and Session 2 overlap, so two engines are needed.
- Session 3 does not overlap with the others, so only one engine is needed for it.
- For the above input, the output is
-
Key Constraints:
- Each session requires exactly one recommendation engine.
- If two or more sessions overlap, they cannot share the same engine.
Approach to Solve the Problem
To solve this problem, we need to determine the maximum number of overlapping sessions at any point in time. This will tell us the minimum number of engines required.