Merge JSON Array Fields
Updated
Problem statement
You are given two compact JSON objects, baseJson and incomingJson. Each object maps a unique lowercase key to an array of integers. Inputs contain no whitespace. Merge every incoming field into the base object using behavior:
"OVERWRITE": an incoming key replaces the complete existing array."APPEND": if the key exists, append the incoming array elements in order; otherwise insert the incoming array.
Fields that occur only in the base object remain unchanged. Return compact JSON with keys in lexicographic order and no whitespace. Preserve array element order and duplicates. The inputs are not modified.
Function
mergeJson(baseJson: String, incomingJson: String, behavior: String) → StringExamples
Example 1
baseJson = "{\"a\":[1,2],\"b\":[3]}"incomingJson = "{\"a\":[4],\"c\":[5]}"behavior = "APPEND"return = "{\"a\":[1,2,4],\"b\":[3],\"c\":[5]}"The incoming value for a is appended, b is preserved, and c is inserted.
Example 2
baseJson = "{\"b\":[3],\"a\":[1,2]}"incomingJson = "{\"a\":[],\"d\":[7]}"behavior = "OVERWRITE"return = "{\"a\":[],\"b\":[3],\"d\":[7]}"The empty incoming array replaces a, and output keys are sorted regardless of input order.
Constraints
- Each object contains at most
10000unique lowercase keys of length1to30. - Each array contains at most
10000integers in[-10^9, 10^9]. - The total number of keys and array elements across both objects is at most
100000. behavioris"OVERWRITE"or"APPEND".- Both input strings are valid compact JSON in the specified subset.
More Tesla Phone Screen questions
- Flatten a Branched List into a Doubly Linked ListPHONE SCREEN · Seen Sep 2026
- Reduce-Based Map and Filter PipelinePHONE SCREEN · Seen Sep 2026
- Resilient Fake API ClientPHONE SCREEN · Seen Sep 2026
- KV Store with RollbackPHONE SCREEN · Seen Apr 2026
- Multi-Channel Valid ConvolutionPHONE SCREEN · Seen Oct 2024
- Course Schedule FeasibilityPHONE SCREEN · Seen Aug 2024
- Average of a Fixed-Size ArrayPHONE SCREEN · Seen Jun 2024
- Swap Even and Odd BitsPHONE SCREEN · Seen Mar 2023
- Find the Intersection Node of Two Linked ListsPHONE SCREEN · Seen Oct 2022
- Priority, Expiration, and LRU EvictionPHONE SCREEN · Seen Mar 2021
Similar questions at other companies
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Business Account KYC Verification — Parts 1–5Stripe · PHONE SCREENStripe interview questions
- Segmented Durable Key-Value StoreOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Device Registry OperationsOnePay · PHONE SCREENOnePay interview questions
- Server Log Summary and Minute AnomaliesNinjaTrader · PHONE SCREEN
- JSON Identity VerificationKikoff · PHONE SCREENKikoff interview questions
- Inventory Discount TrackerRoblox · OARoblox interview questions
- Dictionary Records to an Editable XML TreeSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Hidden ArtifactsIMC Trading · OAIMC Trading interview questions
- LRU CacheAthenahealth · PHONE SCREEN
Tesla interview experiences
- Tesla Experienced Infrastructure SWE Interviews: 1 Experience
- Tesla Older SWE Intern Reports: 14 Experiences (Part 4 of 4)
- Tesla Embedded and SRE Intern Loops: 15 Experiences (Part 3 of 4)
- Tesla SWE and Data Intern Rounds: 10 Experiences (Part 2 of 4)
- Tesla Intern Python Interview Prep Guide
- 2017–2026 Tesla Intern OA / Interviews: 83 Experiences (Part 2 :)