BackmediumStringsCredInfosys

Anagrammed Bookshelf IDs Solution

Problem Statement

A digital archive maintains a registry of N bookshelf identifiers. Each identifier is a string of unique alphanumeric characters. To generate a secure access token for each shelf, the system performs a specific anagrammatic transformation. The token is constructed by concatenating three components: the name of the retrieval tool, the status code of the operation, and the original shelf ID. These three strings are then sorted lexicographically by their character frequency and concatenated into a single string. Your task is to compute this final token for every shelf in the registry.

The input consists of three parallel arrays: tools, statuses, and ids. The array tools contains the names of the tools used to access each shelf. The array statuses contains the result of the access attempt, which is either "success" or "error". The array ids contains the unique identifiers for the shelves. For each index i from 0 to N-1, you must form a combined string S by concatenating tools[i], statuses[i], and ids[i]. The final output for that index is the string formed by sorting all characters in S in ascending ASCII order.

Return an array of strings where the i-th element is the sorted anagram of the concatenated string for the i-th shelf. This process ensures that the resulting token is deterministic and unique to the combination of tool, status, and ID, while obscuring the original structure through character reordering.

Example 1
Input
tools = ["scan", "query"], statuses = ["success", "error"], ids = ["A1B2", "C3D4"]
Output
["12ABacnsssu", "34CDdeorrquy"]

Explanation: For index 0: Concatenate "scan" + "success" + "A1B2" to get "scansuccessA1B2". Sort characters: '1', '2', 'A', 'B', 'a', 'c', 'n', 's', 's', 's', 'u' -> "12ABacnsssu". For index 1: Concatenate "query" + "error" + "C3D4" to get "queryerrorC3D4". Sort characters: '3', '4', 'C', 'D', 'd', 'e', 'o', 'r', 'r', 'q', 'u', 'y' -> "34CDdeorrquy".

Example 2
Input
tools = ["fetch"], statuses = ["success"], ids = ["Z9"]
Output
["9Zefhcsssu"]

Explanation: For index 0: Concatenate "fetch" + "success" + "Z9" to get "fetchsuccessZ9". Sort characters: '9', 'Z', 'e', 'f', 'h', 'c', 's', 's', 's', 'u' -> "9Zefhcsssu".

Example 3
Input
tools = ["get", "put"], statuses = ["error", "success"], ids = ["X1", "Y2"]
Output
["1Xegorr", "2Ypsuccs"]

Explanation: For index 0: Concatenate "get" + "error" + "X1" to get "geterrorX1". Sort characters: '1', 'X', 'e', 'g', 'o', 'r', 'r' -> "1Xegorr". For index 1: Concatenate "put" + "success" + "Y2" to get "putsuccessY2". Sort characters: '2', 'Y', 'p', 's', 'u', 'c', 'c', 's' -> "2Ypsuccs".

Constraints

  • 1 <= N <= 10^5
  • 1 <= tools[i].length <= 10
  • statuses[i] is either "success" or "error
  • 1 <= ids[i].length <= 10
  • All strings consist of alphanumeric characters only
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Anagrammed Bookshelf IDs — Problem Statement & Solution Guide

StringsMediumTOOL RETURNED ID
TimeO(N·L)
|
SpaceO(N)

Problem Description

A digital archive maintains a registry of N bookshelf identifiers. Each identifier is a string of unique alphanumeric characters. To generate a secure access token for each shelf, the system performs a specific anagrammatic transformation. The token is constructed by concatenating three components: the name of the retrieval tool, the status code of the operation, and the original shelf ID. These three strings are then sorted lexicographically by their character frequency and concatenated into a single string. Your task is to compute this final token for every shelf in the registry.

The input consists of three parallel arrays: tools, statuses, and ids. The array tools contains the names of the tools used to access each shelf. The array statuses contains the result of the access attempt, which is either "success" or "error". The array ids contains the unique identifiers for the shelves. For each index i from 0 to N-1, you must form a combined string S by concatenating tools[i], statuses[i], and ids[i]. The final output for that index is the string formed by sorting all characters in S in ascending ASCII order.

Return an array of strings where the i-th element is the sorted anagram of the concatenated string for the i-th shelf. This process ensures that the resulting token is deterministic and unique to the combination of tool, status, and ID, while obscuring the original structure through character reordering.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Anagrammed Bookshelf IDs"

medium

WHY DOES IT MATTER?

Anagram detection exemplifies the "frequency‑based hashing" pattern, turning a combinatorial string problem into a constant‑time key comparison, which is essential for any large‑scale text analytics or duplicate detection task.

OPTIMIZATION CHALLENGE

The breakthrough is realizing that the order of characters is irrelevant; by collapsing each string to its character count vector, you eliminate the need for pairwise sorting or comparison, collapsing quadratic work to linear.

REAL-WORLD CONNECTION

Think of a distributed deduplication service for file signatures: each file's checksum (a fixed‑size hash) replaces the raw content, allowing the system to spot duplicates across data centers with minimal bandwidth.

Always pre‑compute the canonical form once per input and reuse it; avoid recomputing counts inside loops, and choose a fixed‑size array over sorting when the alphabet size is bounded.

COMPLEXITY AT A GLANCE

⏱ Time:O(N·L)
💾 Space:O(N)

Core Theory — Why This Approach?

An anagram is a rearrangement of characters that yields the same multiset of symbols. Detecting anagrams across many strings reduces to comparing their canonical representations – either a sorted character sequence or a fixed‑size frequency vector (e.g., 62‑length for alphanumeric). A naive pairwise comparison would require O(N²·L) time (where L is average length), which quickly becomes infeasible for N up to 10⁵ or larger. The optimal paradigm leverages hashing: compute the canonical key for each transformed identifier in O(L log L) using sorting or O(L) using counting, then store frequencies in a hash map. Identical keys indicate anagrammed tokens, allowing O(N·L) total time and O(N) extra space. This shift from quadratic to linear‑ithmic complexity is the cornerstone of scalable anagram detection.

Interview Questions on This Problem

Q1How would you efficiently determine the number of anagram groups among N transformed bookshelf IDs where each ID is concatenated with a tool name and status code?

Compute a canonical key for each token (sorted characters or a 62‑bucket frequency array), insert the key into a hash map, and increment its count. The number of groups is the number of distinct keys; the size of each group is the map value.

Q2Why is a counting‑array based canonical form preferable to sorting when the character set is limited to alphanumeric characters?

Counting arrays run in O(L) time versus O(L log L) for sorting, and the fixed alphabet size (62) guarantees constant‑time per character, yielding a linear overall algorithm that scales better for long strings.

Q3In a distributed system storing billions of IDs, how can you detect anagram collisions without moving all data to a single node?

Hash each canonical key locally, use a consistent hash partitioner to route identical keys to the same shard, and aggregate counts per shard; a final reduce step merges the partial results, preserving linear scalability.

Examples

Example 1

Input

tools = ["scan", "query"], statuses = ["success", "error"], ids = ["A1B2", "C3D4"]

Output

["12ABacnsssu", "34CDdeorrquy"]

Explanation: For index 0: Concatenate "scan" + "success" + "A1B2" to get "scansuccessA1B2". Sort characters: '1', '2', 'A', 'B', 'a', 'c', 'n', 's', 's', 's', 'u' -> "12ABacnsssu". For index 1: Concatenate "query" + "error" + "C3D4" to get "queryerrorC3D4". Sort characters: '3', '4', 'C', 'D', 'd', 'e', 'o', 'r', 'r', 'q', 'u', 'y' -> "34CDdeorrquy".

Example 2

Input

tools = ["fetch"], statuses = ["success"], ids = ["Z9"]

Output

["9Zefhcsssu"]

Explanation: For index 0: Concatenate "fetch" + "success" + "Z9" to get "fetchsuccessZ9". Sort characters: '9', 'Z', 'e', 'f', 'h', 'c', 's', 's', 's', 'u' -> "9Zefhcsssu".

Example 3

Input

tools = ["get", "put"], statuses = ["error", "success"], ids = ["X1", "Y2"]

Output

["1Xegorr", "2Ypsuccs"]

Explanation: For index 0: Concatenate "get" + "error" + "X1" to get "geterrorX1". Sort characters: '1', 'X', 'e', 'g', 'o', 'r', 'r' -> "1Xegorr". For index 1: Concatenate "put" + "success" + "Y2" to get "putsuccessY2". Sort characters: '2', 'Y', 'p', 's', 'u', 'c', 'c', 's' -> "2Ypsuccs".

Constraints

  • 1 <= N <= 10^5
  • 1 <= tools[i].length <= 10
  • statuses[i] is either "success" or "error
  • 1 <= ids[i].length <= 10
  • All strings consist of alphanumeric characters only

Optimal Approach & Strategy

Compute a frequency‑based key for each token in O(L) time and use a hash map to group identical keys, achieving O(N·L) time.

Brute Force Approach

Compare every pair of tokens character‑by‑character after sorting them, leading to O(N²·L log L) time.

Verified Code Solutions

JavaScript Solution
Time: O(N·L)
/**
 * @param {string[]} tools
 * @param {string[]} statuses
 * @param {string[]} ids
 * @return {string[]}
 */
function generateTokens(tools, statuses, ids) {
    const n = ids.length;
    const result = new Array(n);
    
    for (let i = 0; i < n; i++) {
        const combined = tools[i] + statuses[i] + ids[i];
        const sorted = combined.split('').sort().join('');
        result[i] = sorted;
    }
    
    return result;
}

// Example usage
const tools = ["scan", "query"];
const statuses = ["success", "error"];
const ids = ["A1B2", "C3D4"];

const result = generateTokens(tools, statuses, ids);
result.forEach(token => console.log(token));

Asked in Top Tech Interviews

CredInfosys

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.