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.
Input: ('Today is Tuesday', 't')
Expected Output: 0
Explanation: Matching is case-sensitive, and there are no lowercase 't' characters.
Input: ('', 'x')
Expected Output: 0
Explanation: An empty string contains no characters.
Input: ('a b c', ' ')
Expected Output: 2
Explanation: There are two space characters in the string.
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.
Input: ('', 'only words here')
Expected Output: ['only', 'words', 'here']
Explanation: All words in msg2 are unique because msg1 has no words.
Input: ('Apple banana', 'apple Banana')
Expected Output: ['Apple', 'banana', 'apple', 'Banana']
Explanation: Comparison is case-sensitive, so each differently cased word is considered distinct.
Input: ('red red blue green', 'blue yellow yellow')
Expected Output: ['red', 'green', 'yellow']
Explanation: 'blue' appears in both messages and is excluded. Repeated unique words such as 'red' and 'yellow' are included only once.
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.