Parse emails, hashtags, phone numbers and key-value records from text in R with stringr
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Using base R plus `stringr` only, implement four text-parsing helpers. Each function is vectorized over a character vector and preserves input length and order: a length-0 input gives a length-0 result, and elements that do not match are kept, never dropped.
- `extract_emails(text)` returns a character vector with the first email in each element, lowercased, or `NA_character_` when the element has none. Use the pattern `[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}`.
- `extract_hashtags(text)` returns a list. Each element holds that string's hashtag words in order, lowercased (the token after `#`, made of `[A-Za-z0-9_]`), or `character(0)` when there are none.
- `normalize_phone(text)` strips every non-digit character. If exactly 10 digits remain, it formats them as `"(XXX) XXX-XXXX"`; otherwise it returns `NA_character_`.
- `parse_records(text)` takes elements of the form `name=...; age=...; city=...` (fields always in this order) and returns a data.frame with one row per element: `name` (character, trimmed), `age` (integer) and `city` (character, trimmed).
Examples:
```r
extract_emails(c("Reach me at John.Doe@Example.com", "no email here"))
# c("john.doe@example.com", NA)
extract_hashtags(c("Loving #R and #DataScience", "none"))
# list(c("r", "datascience"), character(0))
normalize_phone(c("Call 415-555-1234", "123"))
# c("(415) 555-1234", NA)
parse_records("name=Ann; age=30; city=Paris")
# data.frame(name = "Ann", age = 30L, city = "Paris")
```
```hint Match the return shape
For each helper, decide whether the output has exactly one value per input element or a variable number of values per element, then pick the `stringr` function that returns that shape.
```
```hint Empty and missing input
Trace each function on `character(0)` and on `NA`. Most `stringr` calls pass them through cleanly, but an `if` whose condition evaluates to NA stops with an error.
```
### Clarifying Questions to Ask
- What should each function return for an `NA` element: `NA`, `character(0)` or an error?
- For a malformed record, should `parse_records` return a row of `NA` values or stop?
- Is an empty field such as `name=;` an empty string or a missing value?
- Is an 11-digit number with a leading country code still rejected, as the exactly-10 rule implies?
### What a Strong Answer Covers
- Keeps every function vectorized and length-preserving, including length-0 input and elements with no match
- Chooses the `stringr` call whose return shape fits each contract: first match, all matches as a list, capture groups
- Applies the phone rule exactly and returns `NA` (no error) for missing input
- Builds the data.frame with the right column types (integer age, trimmed character fields, no factors), including a typed zero-row frame for empty input
- Traces the given examples and names the edge cases the spec leaves open
### Follow-up Questions
- How would you extend `extract_emails` to return all emails in each string?
- How would you make `parse_records` resilient to missing or reordered fields?
- Which inputs would you put in a `testthat` suite for these four functions?
Overview: Write four vectorized R helpers with stringr that pull the first email, every hashtag, a formatted 10-digit phone number and a parsed key-value record out of each string. It tests regex extraction, return shapes that preserve input length, and handling of empty, missing and non-matching input.