PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in JSON parsing, file I/O, HTTP client integration, request/response handling, payload construction, mapping data between arrays (points to colors), and basic error handling.

  • medium
  • Stripe
  • Coding & Algorithms
  • Software Engineer

Implement JSON parsing and HTTP integration tasks

Company: Stripe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are completing an “integration” exercise for a mapping service ("BikeMap"). You may use Google, but you must implement everything in code (you cannot run shell commands like `curl`). ## Given 1. A local file `simple.json` containing GPS points in JSON format: ```json [ {"lat": 37.7749, "lon": -122.4194}, {"lat": 37.7750, "lon": -122.4189} // ... ] ``` 2. A sample shell command (for reference only) that successfully sends a POST request to a BikeMap endpoint. Example form: ```bash curl -X POST "https://api.example.com/bikemap/render" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer <TOKEN>" \ -d '{"mapStyle":"light","paths":[]}' ``` 3. A requirement to update the request payload to include the coordinates from `simple.json`, where each point must also include a color. Assume colors are provided as an input list `colors` with the same length as the list of points (e.g., `colors[i]` corresponds to point `points[i]`). Your outgoing payload should include points in the form: ```json {"lat": 37.7749, "lon": -122.4194, "color": "red"} ``` ## Tasks 1. **Parse and print coordinates**: Read `simple.json` and print the first **10** coordinate pairs in order, one per line as: ``` <lat>,<lon> ``` 2. **POST request without shell**: Using your programming language’s HTTP library, send the POST request equivalent to the provided `curl` example. Print the HTTP status code and response body. 3. **Update payload and re-send**: Create a second request payload that includes the parsed points, each annotated with its corresponding color. Send the POST request again and print the HTTP status code and response body. ## Notes / Constraints - Do not execute shell commands. - Handle basic errors gracefully (file not found, invalid JSON, non-2xx HTTP response). - Assume the endpoint expects JSON and returns a JSON response body.

Quick Answer: This question evaluates proficiency in JSON parsing, file I/O, HTTP client integration, request/response handling, payload construction, mapping data between arrays (points to colors), and basic error handling.

Part 1: Parse and Return the First 10 Coordinate Strings

You are given the contents of a file similar to `simple.json` as a string. The string should contain a JSON array of objects, where each valid object has numeric `lat` and `lon` fields. Parse the JSON and return up to the first 10 valid coordinate pairs in their original order, formatted as `"<lat>,<lon>"`. If the JSON is invalid or the top-level value is not a list, return an empty list. If an item is malformed (not an object, missing a field, or using non-numeric latitude/longitude), skip it.

Constraints

  • `0 <= len(json_text) <= 10^5`
  • The JSON array may contain up to `10^4` items
  • A valid point must be a JSON object with numeric `lat` and `lon` values
  • Use standard Python numeric string conversion when formatting values

Examples

Input: '[{"lat": 37.7749, "lon": -122.4194}, {"lat": 37.7750, "lon": -122.4189}]'

Expected Output: ['37.7749,-122.4194', '37.775,-122.4189']

Explanation: Both points are valid, so both are returned. Note that `37.7750` becomes `37.775` after JSON parsing.

Input: '[{"lat":0,"lon":0},{"lat":1,"lon":-1},{"lat":2,"lon":-2},{"lat":3,"lon":-3},{"lat":4,"lon":-4},{"lat":5,"lon":-5},{"lat":6,"lon":-6},{"lat":7,"lon":-7},{"lat":8,"lon":-8},{"lat":9,"lon":-9},{"lat":10,"lon":-10},{"lat":11,"lon":-11}]'

Expected Output: ['0,0', '1,-1', '2,-2', '3,-3', '4,-4', '5,-5', '6,-6', '7,-7', '8,-8', '9,-9']

Explanation: There are 12 valid points, but only the first 10 should be returned.

Input: 'not json'

Expected Output: []

Explanation: Invalid JSON should be handled gracefully by returning an empty list.

Input: '[{"lat":1,"lon":2},{"lat":3},{"x":9},4,{"lat":5.5,"lon":6.5}]'

Expected Output: ['1,2', '5.5,6.5']

Explanation: Malformed entries are skipped; only valid points are formatted.

Input: '[]'

Expected Output: []

Explanation: An empty list produces an empty result.

Hints

  1. Use `json.loads` to turn the input text into Python objects.
  2. Collect valid points in order and stop once you have 10 of them.

Part 2: Simulate a POST Request Equivalent to cURL

In this problem, real network access is replaced with a mock HTTP server. You must build the POST request that would be equivalent to the given cURL command. Write a function that takes a URL, a bearer token, a payload dictionary, and a `mock_responses` dictionary. The function must: 1. Build a canonical JSON request body using `json.dumps(payload, sort_keys=True, separators=(",", ":"))`. 2. Build the exact request tuple: `(method, url, authorization_header, content_type, body_json)` where: - `method` is `"POST"` - `authorization_header` is `"Bearer <token>"` - `content_type` is `"application/json"` 3. Look up that tuple in `mock_responses`. 4. If found, parse the response body as JSON and return `(status_code, parsed_response)`. 5. If the request is not found, return `(0, {'error': 'request failed'})`. 6. If the response body is not valid JSON, return `(status_code, {'error': 'invalid json response'})`. Non-2xx responses should still be returned normally if their response body is valid JSON.

Constraints

  • `payload` is JSON-serializable
  • `0 <= len(mock_responses) <= 10^4`
  • Request matching must be exact
  • Use canonical JSON with sorted keys and no extra spaces

Examples

Input: ('https://api.example.com/bikemap/render', 'TOKEN123', {'paths': [], 'mapStyle': 'light'}, {('POST', 'https://api.example.com/bikemap/render', 'Bearer TOKEN123', 'application/json', '{"mapStyle":"light","paths":[]}'): (200, '{"ok": true, "id": 7}')})

Expected Output: (200, {'ok': True, 'id': 7})

Explanation: The payload must be serialized canonically so it matches the mock request key.

Input: ('https://api.example.com/bikemap/render', 'BAD', {'mapStyle': 'dark', 'paths': []}, {('POST', 'https://api.example.com/bikemap/render', 'Bearer BAD', 'application/json', '{"mapStyle":"dark","paths":[]}'): (401, '{"error": "unauthorized"}')})

Expected Output: (401, {'error': 'unauthorized'})

Explanation: A non-2xx response is still returned because the response body is valid JSON.

Input: ('https://api.example.com/bikemap/render', 'MISSING', {'mapStyle': 'light', 'paths': []}, {})

Expected Output: (0, {'error': 'request failed'})

Explanation: If no exact request tuple exists in the mock server, the request is treated as failed.

Input: ('https://api.example.com/bikemap/render', 'TOKEN999', {'mapStyle': 'light', 'paths': []}, {('POST', 'https://api.example.com/bikemap/render', 'Bearer TOKEN999', 'application/json', '{"mapStyle":"light","paths":[]}'): (500, 'oops')})

Expected Output: (500, {'error': 'invalid json response'})

Explanation: The request matches, but the response body cannot be parsed as JSON.

Input: ('https://api.example.com/bikemap/render', 'E', {}, {('POST', 'https://api.example.com/bikemap/render', 'Bearer E', 'application/json', '{}'): (200, '{"received": true}')})

Expected Output: (200, {'received': True})

Explanation: An empty payload is serialized as `{}` and can still match successfully.

Hints

  1. Use `json.dumps(..., sort_keys=True, separators=(",", ":"))` so that request matching is deterministic.
  2. Even if the status code is 401 or 500, you should still return it if the response body parses as valid JSON.

Part 3: Add Colors to Points, Build a New Payload, and Re-send

You are given a base payload, a JSON string containing point objects, a list of colors, and a mock HTTP server. Parse the points, attach a color to each point by matching index, place the resulting list under the `paths` key in the payload, and then simulate sending the POST request. Each valid point in the final payload must look like: `{"lat": <lat>, "lon": <lon>, "color": <color>}` Behavior rules: 1. Parse `points_json` as a JSON array. 2. If parsing fails, the top-level value is not a list, or any point is malformed, return `(0, {'error': 'invalid points'})`. 3. If `len(colors) != len(points)`, return `(0, {'error': 'color count mismatch'})`. 4. Create a shallow copy of `base_payload` and replace its `paths` field with the colored points. 5. Serialize the new payload canonically using `json.dumps(..., sort_keys=True, separators=(",", ":"))`. 6. Build the same request tuple format used in Part 2 and look it up in `mock_responses`. 7. If the request is missing, return `(0, {'error': 'request failed'})`. 8. If the response body is invalid JSON, return `(status_code, {'error': 'invalid json response'})`. 9. Otherwise return `(status_code, parsed_response)`. A point is valid only if it is a JSON object with numeric `lat` and `lon` fields.

Constraints

  • `points_json` represents up to `10^4` point objects
  • `colors[i]` corresponds to the point at index `i`
  • `base_payload` is JSON-serializable
  • Canonical JSON serialization must use sorted keys and no extra spaces

Examples

Input: ('https://api.example.com/bikemap/render', 'TOKEN123', {'mapStyle': 'light'}, '[{"lat": 37.7749, "lon": -122.4194}, {"lat": 37.7750, "lon": -122.4189}]', ['red', 'blue'], {('POST', 'https://api.example.com/bikemap/render', 'Bearer TOKEN123', 'application/json', '{"mapStyle":"light","paths":[{"color":"red","lat":37.7749,"lon":-122.4194},{"color":"blue","lat":37.775,"lon":-122.4189}]}'): (201, '{"saved": true, "count": 2}')})

Expected Output: (201, {'saved': True, 'count': 2})

Explanation: The points are parsed, colors are attached by index, and the final request matches the mock server.

Input: ('https://api.example.com/bikemap/render', 'T', {'mapStyle': 'light'}, '[{"lat":1,"lon":2},{"lat":3,"lon":4}]', ['red'], {})

Expected Output: (0, {'error': 'color count mismatch'})

Explanation: There are two points but only one color.

Input: ('https://api.example.com/bikemap/render', 'T', {'mapStyle': 'light'}, 'oops', ['red'], {})

Expected Output: (0, {'error': 'invalid points'})

Explanation: The points input is not valid JSON.

Input: ('https://api.example.com/bikemap/render', 'T', {'mapStyle': 'dark'}, '[{"lat":1,"lon":2}]', ['green'], {})

Expected Output: (0, {'error': 'request failed'})

Explanation: The colored payload is valid, but no matching request exists in the mock server.

Input: ('https://api.example.com/bikemap/render', 'EMPTY', {'mapStyle': 'light'}, '[]', [], {('POST', 'https://api.example.com/bikemap/render', 'Bearer EMPTY', 'application/json', '{"mapStyle":"light","paths":[]}'): (200, '{"saved": true, "count": 0}')})

Expected Output: (200, {'saved': True, 'count': 0})

Explanation: An empty list of points and colors is valid and results in an empty `paths` array.

Hints

  1. First parse all points and verify that the number of colors matches the number of points.
  2. Build a new payload by copying `base_payload`, then overwrite its `paths` field with the colored point list.
Last updated: Apr 23, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Assign Reviewers from Changed Files - Stripe (medium)
  • Generate Account Email Notifications - Stripe (medium)
  • Calculate Transaction Fees - Stripe (medium)
  • Build an Account Transfer Ledger - Stripe (medium)
  • Implement Validation and String Compression - Stripe (hard)