# Greeting Formatter Class

> First impressions are formatted carefully.

Canonical URL: <https://datadriven.io/problems/greeting_formatter_class>

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Implement MessageFormatter(text) with a method sort_by_case() that returns text rearranged so all lowercase letters come first (in their original relative order), followed by all uppercase letters (in their original relative order). Non-letter characters come at the end, in their original relative order. Also implement sort_by_case(text) as a free function that returns MessageFormatter(text).sort_by_case().

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **class design**, **string character classification**, and **stable partitioning**. It checks whether you can implement a simple class with state and a method that reorders characters while preserving relative order within groups.

---

### Break down the requirements

#### Step 1: Store the message in the constructor

The class holds the text as instance state.

#### Step 2: Separate lowercase and uppercase characters

Iterate through the text and collect lowercase characters in one group and uppercase in another, preserving their relative order.

#### Step 3: Concatenate lowercase before uppercase

Join the two groups to produce the reordered result.

---

### The solution

**Class with stable character partitioning by case**

```python
class MessageFormatter:
    def __init__(self, text):
        self.text = text
    def sort_by_case(self):
        lower = []
        upper = []
        for char in self.text:
            if char.islower():
                lower.append(char)
            elif char.isupper():
                upper.append(char)
        result = ''.join(lower) + ''.join(upper)
        return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the text length. Two passes: one to partition, one to join.
> 
> **Space:** O(n) for the two character lists and the joined result.

> **Interviewers Watch For**
>
> Preserving relative order within each group (stable partition). A sort-based approach would work but is O(n log n) and may not preserve the original order of equal-case characters.

> **Common Pitfall**
>
> Including non-alphabetic characters (digits, punctuation) in one of the groups. The prompt says to reorder letters, so non-letter characters need clear handling. Check the test cases for expected behavior.

---

## Common follow-up questions

- What if digits and special characters should be placed after uppercase? _(Tests extending the partitioning to three or more groups.)_
- How would you make this case-insensitive sorting while preserving case? _(Tests using a key function that maps both cases to the same sort bucket.)_
- What if the method should modify the stored text in place? _(Tests updating self.text rather than just returning a new string.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/greeting_formatter_class)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.