FastPrepRepair One Missing Function Punctuation Character

Repair One Missing Function Punctuation Character

Walmart logoWalmart● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Repair the supplied restricted JavaScript declaration source by restoring its one omitted punctuation character.

For this exercise, assume the complete declaration follows function NAME(PARAM) { return PARAM OP INTEGER; }. NAME and PARAM are distinct, nonreserved identifiers with only lowercase ASCII letters. The return expression uses the same declared parameter. OP is exactly +, - or *. INTEGER is canonical decimal 0 through 999, with no leading zeroes except the single literal 0.

Exactly one character from (, ), {, } or ; was omitted. Every identifier, keyword, operator and integer character remains unchanged. For this exercise, an omitted opening parenthesis may join the name and parameter characters into one word. The repeated return parameter still constrains the repaired declaration.

For this exercise, whitespace consists only of ASCII space, tab and newline. The complete declaration has whitespace separating function from NAME and return from PARAM; whitespace is optional at other token boundaries and around the declaration. Multiple insertion positions next to whitespace may produce the same canonical result. The input promises exactly one canonical valid repair under this grammar.

Return exactly function NAME(PARAM) { return PARAM OP INTEGER; }, with the displayed single ASCII spaces and no surrounding whitespace. Preserve the fields. Do not execute code or change the input. The exercise admits no other statements, calls, comments, strings, nested blocks, this, arguments, super, async/generator functions or constructor/hoisting-dependent caller context.

Function

repairRestrictedFunction(source: String) → String

Examples

Example 1

source = "function add(x){return x+1}"return = "function add(x) { return x + 1; }"

Authored example: Insert the omitted semicolon and return the prescribed canonical declaration.

Example 2

source = "function foox){return x+9;}"return = "function foo(x) { return x + 9; }"

Authored example: The omitted opening parenthesis joined foo and x. The repeated return parameter fixes that split.

Example 3

source = "function times(n) return n*3;}"return = "function times(n) { return n * 3; }"

Authored example: The other tokens stay intact; restore the opening brace and normalize whitespace.

Constraints

  • For this exercise, assume 1 <= source.length <= 200, measured in ASCII characters after the omission.
  • The input is derived from a declaration in the stated grammar by deleting exactly one admitted punctuation character, and has exactly one canonical valid repair.
  • 1 <= NAME.length, PARAM.length <= 10; names contain only a through z, are distinct, and are not reserved JavaScript keywords.
  • Whitespace has ASCII codes 32, 9 and 10. Integer text is 0 or one through three digits beginning with 1 through 9.

More Walmart problems

See Walmart hiring insights
public String repairRestrictedFunction(String source) {
    // Write your code here.
    return "";
}
source"function add(x){return x+1}"
expected"function add(x) { return x + 1; }"
Checking account…