# The Style Guide

> Not every word deserves the same treatment.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a string s and an optional list of lowercase exception words, title-case the string: capitalize the first letter of each whitespace-separated word, except that any word in the exceptions list stays lowercase unless it is the very first word of the string (which is always capitalized).

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **string manipulation** with conditional logic based on word position and membership in a stop-word set. Smart title case is a common formatting task that probes whether a candidate handles edge cases like the first word and short connector words.

---

### Break down the requirements

#### Step 1: Define the set of minor words

Words like `a`, `an`, `the`, `and`, `or`, `but`, `in`, `on`, `of`, `for`, `to`, `at` are typically lowercased unless they appear first.

#### Step 2: Split, transform, and rejoin

Split the string on whitespace. Capitalize the first word unconditionally. For subsequent words, capitalize only if they are not in the minor words set.

---

### The solution

**Position-aware capitalization with stop-word set**

```python
def smart_title(s: str) -> str:
    minor = {"a", "an", "the", "and", "or", "but", "in", "on", "of", "for", "to", "at", "by", "is"}
    words = s.split()
    result_words = []
    for i in range(len(words)):
        word = words[i].lower()
        if i == 0 or word not in minor:
            result_words.append(word.capitalize())
        else:
            result_words.append(word)
    result = " ".join(result_words)
    return result
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the total number of characters. Splitting is O(n), and each word operation is proportional to its length.
> 
> **Space:** O(n) for the split words list and result.

> **Interviewers Watch For**
>
> Lowercasing the word before checking the minor set. Without this, `'THE'` would not match `'the'` in the set, producing incorrect capitalization.

> **Common Pitfall**
>
> Using Python's built-in `.title()` method. It capitalizes every word, which is not smart title case. Minor words should remain lowercase.

---

## Common follow-up questions

- What if the last word should also always be capitalized? _(Tests adding an index check for `i == len(words) - 1`.)_
- How would you handle hyphenated words like 'well-known'? _(Tests splitting on hyphens within words and capitalizing each part.)_
- What if the minor word set was configurable per locale? _(Tests parameterizing the function to accept a custom stop-word set.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_style_guide)
- [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.