Quick Overview

Count occurrences of distinct words and return only their frequencies in alphabetical word order. Use a map plus sorted keys, with explicit case sensitivity and attention to empty inputs and string costs.

Return Word Frequencies in Alphabetical Word Order

Company: Abridge

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

Given a list of words, count each distinct word and return a list of counts ordered alphabetically by the corresponding distinct words. The output contains counts only, not word-count pairs. ### Constraints and Clarifications No numerical size bounds are given. For this practice interface, words are case-sensitive strings and alphabetical order means ordinary lexicographic order of their character values; no lowercasing, punctuation removal, or locale collation is applied. An empty list returns an empty list. These comparison choices clarify the interface rather than add reported preprocessing requirements. ### Example For ["pear", "apple", "pear", "banana", "apple", "pear"], the distinct words in order are ["apple", "banana", "pear"], so return [2, 1, 3]. This example illustrates the specified count ordering. ### What a Strong Answer Covers Use a frequency map, sort distinct words rather than frequency values, and construct the output in that key order. Account for repeated words, an empty input, and string comparison costs. ### Follow-up Questions - Why does sorting the counts themselves answer a different question? - How would the requirements change if matching were case-insensitive?

Overview: Count occurrences of distinct words and return only their frequencies in alphabetical word order. Use a map plus sorted keys, with explicit case sensitivity and attention to empty inputs and string costs.

Read the full Abridge Software Engineer interview experience this question came from

Given a list of words, count each distinct word. Return a list containing only the counts, ordered by the corresponding distinct words in ordinary lexicographic order of their character values. Words are case-sensitive strings. Do not lowercase, remove punctuation, normalize, or use locale-specific collation. An empty input returns an empty list. Example: ["pear", "apple", "pear", "banana", "apple", "pear"] has distinct words in order ["apple", "banana", "pear"], so return [2, 1, 3].

Constraints

  • Words are case-sensitive strings; no input-size bound is reported.
  • Sort distinct words lexicographically by character values, without locale collation or normalization.
  • Return counts only, in distinct-word order.
  • An empty word list returns [].

Examples

Input: ([],)

Expected Output: []

Explanation: No distinct words produce no counts.

Input: (['x'],)

Expected Output: [1]

Explanation: The one word appears once.

Hints

  1. The requested order belongs to the words, not the counts.
  2. A repeated word contributes to one count at each of its positions.

Loading coding console...

Show the approach

Approach

Build a frequency map with one entry per distinct word, preserving exact case and punctuation. Sort only its distinct keys by lexicographic character value, then look up each count in that order. Python compares Unicode code points directly. JavaScript and Java use explicit code-point comparators so supplementary characters sort by their character value rather than UTF-16 code-unit order; C++ compares valid UTF-8 bytes as unsigned values, whose lexicographic order matches Unicode scalar values. Every occurrence contributes exactly once to its key, and sorted-key traversal emits exactly one count per distinct key in the required order. The result is empty when there are no keys.

Time complexity:
O(T + K log K * L), where T is the total input character count, K the number of distinct words, and L the maximum comparison length
Space complexity:
O(D + K) auxiliary space, where D is the total length of distinct stored words