Panda Guru LogoPanda
Guru

Amazon SDE I | India | Online Assessment

Round 1

Questions:

Question

Amazon Books is a retail store that sells the newly launched novel "The Story of Amazon". The novel is divided into n volumes numbered from 1 to n and unfortunately, all the volumes are currently out of stock.

The Amazon team announced that starting today, they will bring exactly one volume of "The Story of Amazon" in stock for each of the next n days. On the nth day, all volumes will be in stock. Being an impatient bookworm, each day will purchase the maximum number of volumes you such that:

Note: For the ith volume of the novel, all the volumes j such that j < i are its prequels.

Determine the volumes you would purchase each day. You should return an array of n arrays where the ith array contains:

Examples:

volumes = [2, 1, 4, 3] ans = [[-1], [1, 2], [-1], [3, 4]] volumes = [1, 4, 3, 2, 5] ans = [[1], [-1], [-1], [2, 3, 4], [5]] volumes = [1, 2, 3] ans = [[1], [2], [3]]

Constraints:

Solution:

def buyVolumes(daily_volumes: list[int]) -> list[list[int]]: """ Determines which volumes can be purchased each day based on availability and prerequisites. Args: daily_volumes: List where daily_volumes[i] represents the volume number that becomes available on day i. Returns: List of lists where result[i] contains either: - The volume numbers purchased on day i in ascending order - [-1] if no volumes could be purchased on day i """ total_volumes = len(daily_volumes) # Track which volumes are available for purchase volume_available = [False for _ in range(total_volumes)] # Store the volumes purchased each day daily_purchases = [list() for _ in range(total_volumes)] # Keep track of the next volume we need for sequential reading next_required_volume = 0 for current_day, new_volume in enumerate(daily_volumes): # Mark the new volume as available volume_available[new_volume - 1] = True # Purchase all available sequential volumes while (next_required_volume < total_volumes and volume_available[next_required_volume]): daily_purchases[current_day].append(next_required_volume + 1) next_required_volume += 1 # If no purchases were made today, append -1 if len(daily_purchases[current_day]) == 0: daily_purchases[current_day].append(-1) return daily_purchases
Candidate's Approach

The candidate implemented a function that tracks the availability of volumes and purchases them in sequential order. They used a boolean list to mark available volumes and a list to store daily purchases. The approach efficiently checks for available volumes and handles the case where no volumes can be purchased by appending -1.

Interviewer's Feedback

The interviewer appreciated the clarity of the solution and the efficient use of data structures. They suggested considering edge cases and optimizing for larger inputs.


Question 2

In Amazon's online marketplace, the inventory manager is exploring strategies to enhance product sales. The focus is on creating appealing packages that offer customers a delightful shopping experience. To achieve this, the inventory manager aims to construct packages, each containing at most 2 items, to have equal total cost across all packages. The total cost of a package is defined as the sum of the costs of the items contained within.

Formally, given an array cost of size n, representing the cost of individual items, determine the maximum number of packages that can be created, such that each package adheres to the constraint of having at most 2 items, and all packages have the same cost.

Note that each item can be used in at most one package.

Examples:

cost = [2, 1, 3] ans = 2 cost = [4, 5, 10, 3, 1, 2, 2, 2, 3] ans = 4 cost = [1, 1, 2, 2, 1, 4] ans = 3 cost = [10, 2, 1] ans = 1

Constraints:

Solution:

from collections import defaultdict def findMaximumPackages(cost: list[int]) -> int: """ Finds the maximum number of packages that can be created with equal total costs. Each package can contain at most 2 items. Args: cost: List of individual item costs Returns: Maximum number of packages possible where all packages have equal total cost Example: >>> findMaximumPackages([2, 1, 3]) 2 # Can make two packages: [3] and [1, 2], both with total cost 3 """ # Count frequency of each cost cost_frequency = defaultdict(int) for single_cost in cost: cost_frequency[single_cost] += 1 # Get unique costs unique_costs = list(cost_frequency.keys()) num_unique_costs = len(unique_costs) # Track frequency of each possible package total cost package_cost_frequency = defaultdict(int) # Process each unique cost for i in range(num_unique_costs): current_cost = unique_costs[i] # Single item packages package_cost_frequency[current_cost] += cost_frequency[current_cost] # Two identical items packages package_cost_frequency[2 * current_cost] += (cost_frequency[current_cost] // 2) # Two different items packages for j in range(i + 1, num_unique_costs): other_cost = unique_costs[j] combined_cost = current_cost + other_cost possible_pairs = min(cost_frequency[current_cost], cost_frequency[other_cost]) package_cost_frequency[combined_cost] += possible_pairs # Return the maximum number of packages possible with equal cost return max(package_cost_frequency.values())
Candidate's Approach

The candidate utilized a frequency dictionary to count the occurrences of each cost. They then calculated possible packages by considering single items, pairs of identical items, and pairs of different items. The approach efficiently aggregates the maximum number of packages that can be formed with equal costs.

Interviewer's Feedback

The interviewer commended the candidate for the thoroughness of the solution and the effective use of data structures. They recommended further testing with edge cases to ensure robustness.