Convert a string between snake_case and camelCase

Quick Overview

Write a function that detects whether a string is in snake_case or camelCase and converts it to the other form, returning strings in neither form unchanged. It tests reading a string spec precisely, edge cases such as mixed input and a leading capital, and a single linear scan.

Convert a string between snake_case and camelCase

Company: Ramp

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `s`, convert it between snake_case and camelCase. If `s` is in snake_case (it contains at least one underscore and no uppercase letters), convert it to camelCase by removing all underscores and capitalizing the first letter of each word that follows an underscore. If `s` is in camelCase (it contains at least one uppercase letter and no underscores), convert it to snake_case by inserting an underscore (`_`) before each uppercase letter and converting that letter to lowercase. If `s` is in neither form, return `s` unchanged. That covers an all-lowercase string with no underscores and a string that has both underscores and uppercase letters. ### Function Signature ```python def convert_case(s: str) -> str: ``` ### Rules - The form is decided from the whole string before converting: underscores with no uppercase letters is snake_case, uppercase letters with no underscores is camelCase, and anything else is returned as is. - In a snake_case input the first letter stays lowercase. - In a camelCase input every uppercase letter gets its own underscore, including a letter at index 0 and each letter of a run of capitals. So `"Hello"` becomes `"_hello"` and `"parseHTTP"` becomes `"parse_h_t_t_p"`. ### Constraints - `1 <= len(s) <= 10^4` - `s` consists of lowercase English letters, uppercase English letters, and underscores (`_`). - `s` does not start or end with an underscore. - `s` does not contain consecutive underscores. ### Examples ```text Input: s = "hello_world_foo" Output: "helloWorldFoo" ``` The input has underscores and no capitals, so it is snake_case. Dropping both underscores and capitalizing the "w" and "f" that follow them gives "helloWorldFoo". ```text Input: s = "myVariableName" Output: "my_variable_name" ``` The input has capitals and no underscores, so it is camelCase. "V" becomes "_v" and "N" becomes "_n". ```text Input: s = "get_userId" Output: "get_userId" ``` The string has both an underscore and an uppercase letter, so it is in neither form and comes back unchanged.

Overview: Write a function that detects whether a string is in snake_case or camelCase and converts it to the other form, returning strings in neither form unchanged. It tests reading a string spec precisely, edge cases such as mixed input and a leading capital, and a single linear scan.

Ramp logo
Ramp
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
3
0

Given a string s, convert it between snake_case and camelCase.

If s is in snake_case (it contains at least one underscore and no uppercase letters), convert it to camelCase by removing all underscores and capitalizing the first letter of each word that follows an underscore.

If s is in camelCase (it contains at least one uppercase letter and no underscores), convert it to snake_case by inserting an underscore (_) before each uppercase letter and converting that letter to lowercase.

If s is in neither form, return s unchanged. That covers an all-lowercase string with no underscores and a string that has both underscores and uppercase letters.

Function Signature

def convert_case(s: str) -> str:

Rules

  • The form is decided from the whole string before converting: underscores with no uppercase letters is snake_case, uppercase letters with no underscores is camelCase, and anything else is returned as is.
  • In a snake_case input the first letter stays lowercase.
  • In a camelCase input every uppercase letter gets its own underscore, including a letter at index 0 and each letter of a run of capitals. So "Hello" becomes "_hello" and "parseHTTP" becomes "parse_h_t_t_p" .

Constraints

  • 1 <= len(s) <= 10^4
  • s consists of lowercase English letters, uppercase English letters, and underscores ( _ ).
  • s does not start or end with an underscore.
  • s does not contain consecutive underscores.

Examples

Input: s = "hello_world_foo"
Output: "helloWorldFoo"

The input has underscores and no capitals, so it is snake_case. Dropping both underscores and capitalizing the "w" and "f" that follow them gives "helloWorldFoo".

Input: s = "myVariableName"
Output: "my_variable_name"

The input has capitals and no underscores, so it is camelCase. "V" becomes "_v" and "N" becomes "_n".

Input: s = "get_userId"
Output: "get_userId"

The string has both an underscore and an uppercase letter, so it is in neither form and comes back unchanged.