Use a hash map (dictionary) to count character frequencies while preserving insertion order, then iterate through the map to output the results. In languages like Python, use OrderedDict or rely on dict's insertion order; in Java, use LinkedHashMap. This ensures O(n) time and O(k) space, where k is the number of distinct characters.
Pro tip: Mention that case sensitivity means 'A' and 'a' are distinct, and clarify that the output should list characters in the order they first appear, not sorted alphabetically. Also, discuss edge cases like empty string or Unicode characters to show thoroughness.
Confirm that the output should be ordered by first appearance and that comparison is case-sensitive. Ask about input constraints (e.g., string length, character set) if not specified.
Select an ordered hash map (e.g., LinkedHashMap in Java, OrderedDict in Python) to maintain insertion order while counting frequencies.
Traverse the string once, updating the count for each character in the map. If the character is not present, add it with count 1.
Iterate through the map's entries in order and produce the output, e.g., as a list of (character, count) pairs or a formatted string.
State that time complexity is O(n) and space complexity is O(k), where n is string length and k is number of distinct characters. Mention that this is optimal for a single-pass solution.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.