Question: Amazon | Recent Online Assessment 2026 | Priority Bug Sorting | Package Prefix Division | Cracking the Amazon SDE Coding Round | Frequency Sorting & Prefix Array Logic
2
Entering edit mode

Question 1: Quality Assurance Bug Sorting

Problem Statement: You are helping Amazon's Quality Assurance engineers process bug reports generated from automated testing logs across various devices and services. Each log contains an integer bug code, and a single test session may include duplicate bug codes if the same issue is triggered multiple times.

To effectively prioritise debugging and resolution, the following rules are applied:

  • Less frequent bugs are considered more important, as they may indicate rare or edge-case issues.
  • If two bugs occur the same number of times, the bug with the lower code number has higher priority.

The task is to sort the bug codes in order of decreasing importance, using the above rules.

Example: bugs = [8, 4, 6, 5, 4, 8]

Item Code Frequency:

  • 8: 2 occurrences
  • 4: 2 occurrences
  • 6: 1 occurrence
  • 5: 1 occurrence

Question 2: Package Sorting Optimization (Prefix Division)

Problem Statement:

A team at Amazon is working to ensure all packages are correctly sorted for delivery. Each package has a label represented by an uppercase English letter. The full list of labels is given as string packages, where the ith character is the label of the i^{th} package.

To optimize the sorting process, the team wants to analyze each prefix of the string packages (from length 1 to n) and determine the maximum number of equal parts it can be divided into.

Each part must satisfy the following condition:

  • Each part must have the same frequency of every character as every other part in that division.

Given a string packages, calculate for each prefix t (from length 1 to n), the maximum number of equal parts into which the prefix can be divided, such that each part has the same number of occurrences of each character.

Example:

n = 4

packages = "ABAB"

In the given example, t represents the prefix string and length represents the length of the prefix string.

Explanation Table:

  • length = 1, t = A: A single-character string (length=1) cannot be divided. (Maximum parts = 1)
  • length = 2, t = AB: For the count of each character to be the same, the string cannot be divided. (Maximum parts = 1)
  • length = 3, t = ABA: For the count of each character to be the same, the string cannot be divided. (Maximum parts = 1)
  • length = 4, t = ABAB: The string can be divided into 2 equal parts: "AB" and "AB". (Maximum parts = 2)
ADD COMMENTlink 2 days ago admin 1.9k
Entering edit mode
2

Problem1 -> 

Quality Assurance Bug Sorting Solution

Topics Involved / Prerequisites

  • Hash Maps (Frequency Counting)
  • Custom Sorting (Comparators)

Overview

To sort the bugs according to the QA team's specific rules, we need to know exactly how many times each bug appears before we rearrange them. By first counting the frequencies of all bug codes, we can then pass a custom sorting rule to our algorithm: sort primarily by frequency in ascending order, and break any ties by sorting the bug codes themselves in ascending order.

Approach

1. Counting Frequencies

We traverse the array once and use a Hash Map (unordered_map in C++) to keep a running tally of how many times each bug code appears in the logs.

2. Custom Sorting Logic

We use the standard sorting function but provide it with a custom lambda function (comparator). When the sorting algorithm compares two bug codes, a and b, our custom rule dictates:

  • If freq[a] is not equal to freq[b], place the one with the smaller frequency first.
  • If freq[a] equals freq[b], place the one with the smaller code value first.
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>

using namespace std;

vector<int> sortBugs(vector<int>& bugs) {
    unordered_map<int, int> freq;
    
    // Step 1: Count the frequency of each bug code
    for (int bug : bugs) {
        freq[bug]++;
    }
    
    // Step 2: Sort using a custom lambda comparator
    sort(bugs.begin(), bugs.end(), [&](int a, int b) {
        if (freq[a] != freq[b]) {
            return freq[a] < freq[b]; // Less frequent first
        }
        return a < b; // Lower code number first on tie
    });
    
    return bugs;
}

int main() {
    vector<int> bugs = {8, 4, 6, 5, 4, 8};
    vector<int> sortedBugs = sortBugs(bugs);
    
    cout << "Sorted Bugs: ";
    for (int bug : sortedBugs) {
        cout << bug << " "; 
    }
    // Output: 5 6 4 4 8 8
    cout << "\n";
    
    return 0;
}

Time Complexity

  • Time: O(N log N) - Counting the frequencies takes linear time, but sorting the array dictates the overall time complexity.
  • Space: O(N) - To store the frequencies in the hash map.
ADD REPLYlink 2 days ago
admin
1.9k
Entering edit mode
0

Problem2 

Package Sorting Optimization Solution

Topics Involved / Prerequisites

  • String Traversal

  • Greatest Common Divisor (GCD) Math

Overview

This problem initially sounds like a complex string-partitioning challenge, but there is a brilliant mathematical catch! If you want to divide a collection of characters into K parts such that every part has the exact same character counts, then the total frequency of every character must be perfectly divisible by K. Therefore, the maximum number of parts K is simply the Greatest Common Divisor (GCD) of all the current character frequencies.

Approach

1. Tracking Frequencies on the Fly

Instead of processing each prefix from scratch (which would be too slow), we iterate through the string character by character. We maintain an array of size 26 to continuously track the frequencies of the uppercase letters as our prefix grows.

2. Calculating the GCD

For every new character we add to our prefix, we want to find the maximum possible divisions. We do this by calculating the GCD of all the non-zero frequencies currently in our tracking array. We append this GCD to our results array and move to the next character.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

// Helper function to calculate GCD of two numbers
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

vector<int> maxEqualParts(string packages) {
    int n = packages.length();
    vector<int> result;
    vector<int> freq(26, 0);
    
    for (int i = 0; i < n; i++) {
        // Update the frequency of the current character
        freq[packages[i] - 'A']++;
        
        int current_gcd = 0;
        
        // Find the GCD of all non-zero frequencies
        for (int count : freq) {
            if (count > 0) {
                current_gcd = gcd(current_gcd, count);
            }
        }
        
        result.push_back(current_gcd);
    }
    
    return result;
}

int main() {
    string packages = "ABAB";
    vector<int> ans = maxEqualParts(packages);
    
    cout << "Maximum parts per prefix: ";
    for (int parts : ans) {
        cout << parts << " ";
    }
    // Output: 1 1 1 2
    cout << "\n";
    
    return 0;
}

 

ADD REPLYlink 2 days ago
admin
1.9k

Login before adding your answer.

Similar Posts
Loading Similar Posts