Just did an online screening with Tesla, they ask a variation on Basic Calculator 2/3
- Posted
- 11 Apr 2025
Summary
I just completed an online screening with Tesla where I was presented with a detailed problem requiring the implementation of a JSONPath Evaluator, which felt like a variation on the Basic Calculator problems.
How it went
I underwent an online screening with Tesla. The core of the interview revolved around solving a complex programming challenge. The problem presented was titled 'JSONPath Evaluator' and required me to implement a function capable of parsing and evaluating JSONPath expressions against a given JSON object, similar in complexity to advanced calculator problems.
Questions asked (1)
- 01
JSONPath Evaluator
Data Structures & Algorithms · Hard
Problem Description
Implement a function that evaluates JSONPath expressions to extract data from a JSON object. JSONPath is a query language for JSON, similar to how XPath is used for XML.
Your task is to implement the
evalJq(json_data, expression)function that takes a JSON object and a JSONPath expression and returns the corresponding value(s) from the JSON data.JSONPath Expression Syntax
Your implementation should support the following JSONPath features:
$- The root object/element.property- Child operator (access direct properties)[index]- Array index expressions to access array elements[start:end]- Array slice operator to get a range of elements
Examples
For the following JSON data:
{ "traveler": "Alex Johnson", "passport": "AB123456", "mileagePoints": 75000, "homeAirport": { "code": "SFO", "name": "San Francisco International", "country": "USA" }, "recentFlights": [ { "flightNumber": "UA456", "from": "SFO", "to": "JFK", "date": "2025-03-15", "class": "business" }, { "flightNumber": "LH789", "from": "JFK", "to": "FRA", "date": "2025-03-20", "class": "economy" }, { "flightNumber": "BA321", "from": "FRA", "to": "LHR", "date": "2025-03-22", "class": "premium" } ] }The function should return:
- `evalJq(data, "$")` → The entire JSON object - `evalJq(data, "$.traveler")` → "Alex Johnson" - `evalJq(data, "$.homeAirport.code")` → "SFO" - `evalJq(data, "$.mileagePoints")` → 75000 - `evalJq(data, "$.recentFlights[0].flightNumber")` → "UA456" - `evalJq(data, "$.recentFlights[2].class")` → "premium" - `evalJq(data, "$.recentFlights[:].to")` → ["JFK", "FRA", "LHR"]Constraints
- The input JSON data will be a valid JSON object.
- The JSONPath expression will be a valid string following the syntax described above.
- You must handle nested objects and arrays.
- For array slice notation
[start:end], if eitherstartorendis missing, it should be treated as the start or end of the array respectively.