Format and Validate a Model Response
Updated
Problem statement
Write a response formatter and a separate validator, and combine them in formatAndValidateModelResponse. You are given returned model text response and a required-field schema whose inclusive value ranges are minimums and maximums.
How the response is written
- 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, bad names, bad numbers or bad line 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
In the onsite, candidates called OpenAI with a scenario-specific prompt and an interviewer-supplied API key; one function formats the returned output and a second checks it, in Python or TypeScript. This version starts from the response text you are given and uses the rules above. You do not make API calls. Prompt quality, remote errors and model behavior are not part of the task.
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"]The lines are trimmed. beta : 002 becomes beta=2 and alpha : -0 becomes alpha=0. The names are sorted, so alpha comes before beta. The value 2 is in the range 1 to 3 set for beta, and 0 is in the range 0 to 0 set for alpha. The answer is ["alpha=0\nbeta=2","true"].
Example 2
response = "a:2\nb:9"requiredFields = ["a","b"]minimums = [0,0]maximums = [3,5]return = ["a=2\nb=9","false"]Both lines are well formed, so the formatted text is kept. The value for a is 2, which is in the range 0 to 3. The value for b is 9, which is above its maximum of 5. The answer is ["a=2\nb=9","false"].
Example 3
response = "a:1\na:2"requiredFields = ["a"]minimums = [0]maximums = [5]return = ["","false"]The same case-sensitive name a appears twice, so formatting fails and validation is false. The answer is ["","false"].
Constraints
- Here,
0 <= response.length <= 20000; all characters are ASCII. - Here, at most 1000 nonblank raw lines; their record syntax may be invalid.
- Here,
0 <= requiredFields.length <= 100and 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 Onsite Interview questions
- Task Scheduler with DependenciesONSITE INTERVIEW · Seen Jul 2026
- Best Poker Category with a JokerPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2026
- Stateful Neuron Matrix UpdateONSITE INTERVIEW · Seen Apr 2026
- Clock Hand Angle with Subsecond PrecisionONSITE INTERVIEW · Seen Dec 2025
- Four-Player Matching Card GameONSITE INTERVIEW · Seen Feb 2025
- Rotate an Array to the RightONSITE INTERVIEW · Seen Oct 2024
- Rich-Text Segment Normalization and Bold ToggleONSITE INTERVIEW · Seen Apr 2024
- Minimum Distance Matching in a ForestPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2024
- Shortest Distance Between Two NodesONSITE INTERVIEW · Seen Jan 2024
- Fit Beta Parameters by Maximum LikelihoodONSITE INTERVIEW · Seen Sep 2022
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
- Group AnagramsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Most Frequent Call Stack Per ThreadRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Build an Inverted Document IndexConfluent · ONSITE INTERVIEWConfluent interview questions
- Find Duplicate Element PairsGoogle · ONSITE INTERVIEWGoogle interview questions
- Filesystem Hash TreeCursor · PHONE SCREEN · ONSITE INTERVIEWCursor interview questions
- Sort Characters by FrequencySnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions