PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in string manipulation and text processing, including character counting, tokenization by spaces, case-sensitive comparisons, and preserving original word order.

  • medium
  • xAI
  • Coding & Algorithms
  • Data Engineer

Implement two string utility functions

Company: xAI

Role: Data Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Implement the following Python functions: 1. `number_of_character(string, char)` - Return the number of times character `char` appears in `string`. - Matching should be case-sensitive. - Example: `number_of_character("Today is Tuesday", "t")` should return `2`. 2. `unique_string(msg1, msg2)` - Split each input message into words using spaces. - Return the words that appear in `msg1` but not in `msg2`, followed by the words that appear in `msg2` but not in `msg1`. - Preserve original order. - Use case-sensitive comparison. - Example: - `msg1 = "This is the first message"` - `msg2 = "Hello this is the second message"` - Output: `["This", "first", "Hello", "this", "second"]`

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

Implement a function that returns how many times a given character appears in a string. Matching is case-sensitive, so uppercase and lowercase letters are treated as different characters. For example, counting 'T' in 'Today is Tuesday' returns 2.

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

  1. Scan the string one character at a time and compare each character to char.
  2. Be careful not to convert the string to lowercase or uppercase; matching must be case-sensitive.

Part 2: Find Words Unique to Each Message

Implement a function that compares two messages and returns the words that appear in exactly one of the messages. First return the distinct words that appear in msg1 but not in msg2, preserving their first-appearance order in msg1. Then append the distinct words that appear in msg2 but not in msg1, preserving their first-appearance order in msg2. Word comparison is case-sensitive. Multiple repeated copies of the same unique word in one message should appear only once in the output.

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

  1. Use sets to quickly check whether a word from one message appears in the other message.
  2. Build the answer in two passes: first over msg1, then over msg2. Track words already added to avoid duplicates.
Last updated: Jun 20, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Flatten and unflatten nested Python structures - xAI (nan)
  • Compute dasher pay from order events - xAI (nan)
  • Compute total active time per Twitter Space - xAI (medium)
  • Design a Recoverable Iterator - xAI (medium)
  • Implement Distributed Matrix Multiplication - xAI (hard)