Implement two string utility functions
Company: xAI
Role: Data Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in string manipulation and text processing, including character counting, tokenization by spaces, case-sensitive comparisons, and preserving original word order.
Part 1: Count Occurrences of a Character
Constraints
- 0 <= len(string) <= 100000
- len(char) == 1
- Matching is case-sensitive.
- string may contain letters, digits, spaces, or punctuation.
Examples
Input: ('banana', 'a')
Expected Output: 3
Explanation: The character 'a' appears three times in 'banana'.
Input: ('Today is Tuesday', 'T')
Expected Output: 2
Explanation: There are two uppercase 'T' characters.
Hints
- Scan the string one character at a time and compare each character to char.
- Be careful not to convert the string to lowercase or uppercase; matching must be case-sensitive.
Part 2: Find Words Unique to Each Message
Constraints
- 0 <= len(msg1), len(msg2) <= 100000
- Messages contain words separated by spaces.
- Word comparison is case-sensitive.
- If a word appears in both messages exactly, it should not be included in the result.
- If the same unique word appears multiple times in one message, include it only once at its first occurrence.
Examples
Input: ('This is the first message', 'Hello this is the second message')
Expected Output: ['This', 'first', 'Hello', 'this', 'second']
Explanation: 'This' and 'first' appear only in msg1. 'Hello', 'this', and 'second' appear only in msg2. Case matters, so 'This' and 'this' are different.
Input: ('', '')
Expected Output: []
Explanation: Both messages are empty, so there are no unique words.
Hints
- Use sets to quickly check whether a word from one message appears in the other message.
- Build the answer in two passes: first over msg1, then over msg2. Track words already added to avoid duplicates.