Just did an online screening with Tesla, they ask a variation on Basic Calculator 2/3

tesla logo
tesla
April 11, 20253 reads

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.

Full Experience

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.

Interview Questions (1)

Q1
JSONPath Evaluator
Data Structures & AlgorithmsHard

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:

  1. $ - The root object/element
  2. .property - Child operator (access direct properties)
  3. [index] - Array index expressions to access array elements
  4. [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 either start or end is missing, it should be treated as the start or end of the array respectively.
Discussion (0)

Share your thoughts and ask questions

Join the Discussion

Sign in with Google to share your thoughts and ask questions

No comments yet

Be the first to share your thoughts and start the discussion!