Convert a Restricted Function to an Arrow Function
Updated
Problem statement
You are given source, a JavaScript function declaration. Turn it into the arrow-function string described below.
The input has this shape: function NAME(PARAM) { return PARAM OP INTEGER; }
NAMEandPARAMare different identifiers made of lowercase ASCII letters only. Neither is a reserved word.- The same parameter appears in the parameter list and in the return expression.
OPis exactly+,-or*.INTEGERis a nonnegative whole number from0through999in canonical decimal form: no leading zeroes, except for the number0itself.
Whitespace here means only ASCII space, tab and newline. It may appear around the declaration or between tokens. There is at least one whitespace character between function and NAME, and between return and PARAM. Everywhere else, punctuation may touch the tokens next to it with no space.
Return exactly const NAME = (PARAM) => PARAM OP INTEGER;, with one ASCII space at each space shown and no whitespace at the start or end. Keep the name, parameter, operator and number text as they are. Do not run the code or change the input.
The input holds only this one pure return expression. It has no calls, comments, strings, nested statements, this, arguments, super, async or generator functions, or caller context that depends on constructors or declaration hoisting.
Function
convertRestrictedFunctionToArrow(source: String) → StringExamples
Example 1
source = "function add(x) { return x + 1; }"return = "const add = (x) => x + 1;"Keep the name add, the parameter x and the addition, and write them in the exact arrow form.
Example 2
source = "function lower(value){return value-999;}"return = "const lower = (value) => value - 999;"Here punctuation touches the tokens with no spaces. The output still has every space the arrow form requires.
Example 3
source = "\tfunction\nscale ( amount )\n{\treturn\tamount*0 ;\n}\t"return = "const scale = (amount) => amount * 0;"The input uses spaces, tabs and newlines as separators. They all become the fixed spacing of the output. Multiplying by zero is not simplified: it stays in the output as written.
Constraints
1 <= source.length <= 200, counted in ASCII characters.sourceis a valid declaration that follows the shape and the input rules in the statement.1 <= NAME.length, PARAM.length <= 10. Names contain onlyathroughz, are different from each other, and are not reserved JavaScript keywords.- Whitespace characters have ASCII codes
32,9and10. INTEGERis0, or a decimal number of one, two or three digits starting with1through9.
More Walmart Phone Screen questions
- Count Months Starting on SundayPHONE SCREEN · Seen Aug 2026
- Longest Even-Length WordPHONE SCREEN · Seen Aug 2026
- Range ModulePHONE SCREEN · Seen Aug 2026
- Meeting Rooms IIPHONE SCREEN · Seen Aug 2026
- Sort Real Number StringsPHONE SCREEN · Seen Jan 2026
- Check a Password Against a Supplied PolicyPHONE SCREEN · Seen Jun 2025
- Repair One Missing Function Punctuation CharacterPHONE SCREEN · Seen Jun 2025
- Can the Singer Perform the Song?PHONE SCREEN · Seen Mar 2025
- Rooms With Two Other Entrances and a Treasure SuccessorPHONE SCREEN · Seen Mar 2025
- First Dictionary Word from Note LettersPHONE SCREEN · Seen Aug 2022
Similar questions at other companies
- Run-Length Encoding and DecodingAirbnb · PHONE SCREENAirbnb interview questions
- Straight-Line Word SearchReddit · PHONE SCREENReddit interview questions
- Search and Format ContactsAdyen · PHONE SCREENAdyen interview questions
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Transaction Fee Calculator — Parts 1–2: Basic Fee Calculation and Conditional Fee RulesStripe · PHONE SCREENStripe interview questions
- Valid ParenthesesByteDance · PHONE SCREENByteDance interview questions
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- Check if a String Is a PalindromeCisco · PHONE SCREENCisco interview questions
- Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Longest Common PrefixKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions