FastPrepFormat and Validate a Model Response

Format and Validate a Model Response

Scale AI logoScale AI● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

For this practice exercise, implement a response formatter and a separate validator, then compose them in formatAndValidateModelResponse. You are given returned model text response and a required-field schema whose inclusive value ranges are minimums and maximums.

Returned-response grammar

  • Split the text at every LF character. A raw line may end with one CR character; remove that terminal CR, then trim ASCII spaces and tabs from both ends. Ignore a line that is now empty.
  • Each remaining line must contain exactly one colon: name : signedDecimal. Trim ASCII spaces and tabs around the name and number. Internal whitespace, other separators and additional CR characters are invalid.
  • A name has 1 to 32 characters, starts with an ASCII letter, and thereafter contains only ASCII letters, digits or underscores. Names are case-sensitive.
  • A number contains an optional + or - followed by at least one ASCII digit. Leading zeros are allowed, even a long sequence of zeros. Its numeric value must lie in [-1000000000,1000000000].
  • Repeated names, malformed names/numbers or malformed record syntax make formatting fail.

Formatter

If any record is invalid, return ["","false"]. Otherwise sort the unique names in ascending ASCII lexicographic order. Join name=value records with LF and no trailing LF, using ordinary decimal spelling with no plus sign or leading zeros. Both signed zero spellings become 0. An empty set of records formats as an empty string.

Validator

Validate that formatted result in a second function. It is valid exactly when its names are the required fields with no missing or extra name, and the value for requiredFields[i] lies in [minimums[i],maximums[i]]. Schema order determines each field's range; it does not determine output order.

Return [formattedText,"true"] when validation passes, or [formattedText,"false"] otherwise. A well-formed result that fails schema or range validation keeps its formatted text. An empty schema validates only an empty formatted result.

Interview context

The original onsite report describes calling OpenAI with a scenario-specific prompt and an interviewer-supplied API key. One function formats the returned output and a second checks that output; Python and TypeScript were mentioned for the practical round. The report does not preserve its scenario, response grammar or validation rules. This deterministic practice starts at the explicitly supplied returned-response boundary and uses the rules above. Actual API calls, prompt quality, remote errors and model behavior are outside the judged interface.

Function

formatAndValidateModelResponse(response: String, requiredFields: String[], minimums: int[], maximums: int[]) → String[]

Examples

Example 1

response = " beta : 002\nalpha : -0"requiredFields = ["beta","alpha"]minimums = [1,0]maximums = [3,0]return = ["alpha=0\nbeta=2","true"]

Formatting sorts alpha before beta, normalizes -0 and leading zeros, and validates each value against the range paired with its original schema position.

Example 2

response = "a:2\nb:9"requiredFields = ["a","b"]minimums = [0,0]maximums = [3,5]return = ["a=2\nb=9","false"]

Both records have valid syntax. The formatted text is retained, but b=9 exceeds its maximum5.

Example 3

response = "a:1\na:2"requiredFields = ["a"]minimums = [0]maximums = [5]return = ["","false"]

The same case-sensitive field name occurs twice, so formatting fails and validation is false.

Constraints

  • For this exercise, assume 0 <= response.length <= 20000; all characters are ASCII.
  • For this exercise, assume at most 1000 nonblank raw lines; their record syntax may be invalid.
  • For this exercise, assume 0 <= requiredFields.length <= 100 and all three schema arrays have equal length.
  • The supplied schema is valid: required names are unique and obey the name grammar; each minimums[i] <= maximums[i] and both are in [-1000000000,1000000000].

More Scale AI problems

See Scale AI hiring insights
public String[] formatAndValidateModelResponse(String response, String[] requiredFields, int[] minimums, int[] maximums) {
    // write your code here
}
response" beta : 002\nalpha : -0"
requiredFields["beta","alpha"]
minimums[1,0]
maximums[3,0]
expected["alpha=0\nbeta=2", "true"]
Checking account…