Snake case to camel case, then across a whole repository
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's first round (coding), last asked Apr 2026. It tests strings, refactoring and tooling.
Two parts: a small function, then an open discussion about using it on a very large codebase.
Part 1: convert one name
Convert a valid snake_case identifier to lowerCamelCase. The input is one or more lowercase ASCII words joined by single underscores, with no leading, trailing or doubled underscores. Keep the first word as is, drop the underscores, and capitalise the first letter of every later word.
Unless you agree on a different interface with the interviewer, assume:
to_camel(name: str) -> str
to_camel("foo_bar") -> "fooBar"
to_camel("already") -> "already"
to_camel("http_api_name") -> "httpApiName"
to_camel("a") -> "a"
Constraints: 1 <= len(name) <= 10^5; name matches [a-z]+(_[a-z]+)*.
Part 2: rename a whole repository
A huge repository names its variables and functions in snake_case. The team wants camelCase everywhere, and the change is far too big for anyone to review by hand. How would you use your function to rename everything safely?
Clarifying questions
- Part 1: may I rely on the input being valid, or should I validate it?
- Part 2: which languages? Are there public APIs, serialised field names, database columns or config keys that other systems depend on?
- Part 2: can the change land in stages, or must it be one commit?
- Part 2: what tests and builds exist to catch breakage?
What a strong answer covers
- Part 1: one pass that capitalises the character after each underscore;
O(n)with a string builder rather than repeated concatenation. - Part 2: never rename by text search. Use the language's parser or a refactoring tool to find identifier definitions and every reference, so strings, comments and unrelated names are untouched.
- Collisions: two names that map to the same camelCase name (
foo_barand an existingfooBar), and reserved words. - Boundaries that must not change: public APIs, reflection, serialisation, generated code, third-party code. Keep aliases or deprecation shims where needed.
- Rollout: split by module, run builds and tests per batch, automated checks that the program's behaviour is unchanged, and a codemod that can be rerun as the code moves.
- Review strategy: review the tool and a sample of the diff, not every line.
Follow-ups
- Convert back from camelCase to snake_case. What about acronyms like
HTTPServer? - Handle invalid input (leading underscores, digits, double underscores) with defined rules.
- How do you keep new snake_case names from coming back after the migration?
Where it's asked
- Google · First round: coding · Last asked Apr 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
strings · refactoring · tooling
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
