👽
Software Engineer Interview Handbook
  • README
  • Behavioral
    • Useful Links
    • Dongze Li
  • Algorithm
    • Segment Tree
    • Array
      • Product Of Array Except Self
      • Merge Strings Alternately
      • Increasing Triplet Subsequence
      • String Compression
      • Greatest Common Divisor Strings
      • Max Product Of Three
      • Find Duplicate Num
      • Valid Palindrome Ii
      • Next Permutation
      • Rearrange Array By Sign
      • Removing Min Max Elements
      • Find Original Array From Doubled
      • Reverse Words Ii
    • Backtracking
      • Letter Combination Phone Number
      • Combination Sum Iii
      • N Queens
      • Permutations
      • Combination Sum
    • Binary Search
      • Koko Eating Bananas
      • Find Peak Element
      • Successful Pairs Of Spells Potions
    • Binary Search Tree
      • Delete Node In BST
      • Validate Bst
      • Range Sum Bst
    • Binary Tree
      • Maximum Depth
      • Leaf Similar Trees
      • Maximum Level Sum
      • Binary Tree Right Side
      • Lowest Common Ancestor
      • Longest Zigzag Path
      • Count Good Nodes
      • Path Sum III
      • Maximum Path Sum
      • Move Zero
      • Diameter Binary Tree
      • Sum Root Leaf Number
      • Traversal
      • Binary Tree Vertical Order
      • Height Tree Removal Queries
      • Count Nodes Avg Subtree
      • Distribute Coins
      • Binary Tree Max Path Sum
    • Bit
      • Min Flips
      • Single Number
      • Pow
      • Find Unique Binary Str
    • BFS
      • Rotten Oranges
      • Nearest Exist From Entrance
      • Minimum Knight Moves
      • Network Delay Time
      • Minimum Height Tree
      • Knight Probability In Board
    • Design
      • LRU Cache
      • Get Random
      • LFU Cache
      • Moving Average
      • Rle Iterator
      • Design Hashmap
    • DFS
      • Reorder Routes Lead City
      • Evaluate Division
      • Keys And Rooms
      • Number Of Provinces
      • Disconnected Path With One Flip
      • Course Schedule Ii
      • Robot Room Cleaner
      • Word Break Ii
      • Number Coins In Tree Nodes
      • Maximum Increasing Cells
      • Number Coins In Tree Nodes
      • Detonate Maximum Bombs
      • Find All Possible Recipes
      • Min Fuel Report Capital
      • Similar String Groups
    • DP
      • Domino And Tromino Tiling
      • House Robber
      • Longest Common Subsequence
      • Trade Stock With Transaction Fee
      • Buy And Sell Stock
      • Longest Non Decreasing Subarray
      • Number Of Good Binary Strings
      • Delete And Earn
      • Minimum Costs Using Train Line
      • Decode Ways
      • Trapping Rain Water
      • Count Fertile Pyramids
      • Minimum Time Finish Race
      • Knapsack
      • Count Unique Char Substrs
      • Count All Valid Pickup
    • Greedy
      • Dota2 Senate
      • Smallest Range Ii
      • Can Place Flowers
      • Meeting Rooms II
      • Guess the word
      • Minimum Replacement
      • Longest Palindrome Two Letter Words
      • Parentheses String Valid
      • Largest Palindromic Num
      • Find Missing Observations
      • Most Profit Assigning Work
    • Hashmap
      • Equal Row Column Pairs
      • Two Strings Close
      • Group Anagrams
      • Detect Squares
    • Heap
      • Maximum Subsequence Score
      • Smallest Number Infinite Set
      • Total Cost Hire Workers
      • Kth Largest Element
      • Meeting Rooms III
      • K Closest Points Origin
      • Merge K Sorted List
      • Top K Frequent Elements
      • Meeting Room III
      • Num Flowers Bloom
      • Find Median From Stream
    • Intervals
      • Non Overlapping Intervals
      • Min Arrows Burst Ballons
    • Linkedlist
      • Reverse Linked List
      • Delete Middle Node
      • Odd Even Linkedlist
      • Palindrome Linkedlist
    • Monotonic Stack
      • Daily Temperatures
      • Online Stock Span
    • Random
      • Random Pick With Weight
      • Random Pick Index
      • Shuffle An Array
    • Recursion
      • Difference Between Two Objs
    • Segment Fenwick
      • Longest Increasing Subsequence II
    • Stack
      • Removing Stars From String
      • Asteroid Collision
      • Evaluate Reverse Polish Notation
      • Building With Ocean View
      • Min Remove Parentheses
      • Basic Calculator Ii
      • Simplify Path
      • Min Add Parentheses
    • Prefix Sum
      • Find The Highest Altitude
      • Find Pivot Index
      • Subarray Sum K
      • Range Addition
    • Sliding Window
      • Max Vowels Substring
      • Max Consecutive Ones III
      • Longest Subarray Deleting Element
      • Minimum Window Substring
      • K Radius Subarray Averages
    • String
      • Valid Word Abbreviations
    • Two Pointers
      • Container With Most Water
      • Max Number K Sum Pairs
      • Is Subsequence
      • Num Substrings Contains Three Char
    • Trie
      • Prefix Tree
      • Search Suggestions System
      • Design File System
    • Union Find
      • Accounts Merge
    • Multithreading
      • Basics
      • Web Crawler
  • System Design
    • Operating System
    • Mocks
      • Design ChatGPT
      • Design Web Crawler
      • Distributed Search
      • News Feed Search
      • Top K / Ad Click Aggregation
      • Design Job Scheduler
      • Distributed Message Queue
      • Google Maps
      • Nearby Friends
      • Proximity Service
      • Metrics monitoring and alert system
      • Design Email
      • Design Gaming Leaderboard
      • Facebook New Feed Live Comments
      • Dog Sitting App
      • Design Chat App (WhatsApp)
      • Design Youtube/Netflix
      • Design Google Doc
      • Design Webhook
      • Validate Instacart Shopper Checkout
      • Design Inventory
      • Design donation app
      • Design Twitter
    • Deep-Dive
      • Back of Envelope
      • Message Queue
      • Redis Sorted Set
      • FAQ
      • Geohash
      • Quadtree
      • Redis Pub/Sub
      • Cassandra DB
      • Collaborative Concurrency Control
      • Websocket / Long Polling / SSE
    • DDIA
      • Chapter 2: Data Models and Query Languages
      • Chapter 5: Replication
      • Chapter 9: Consistency and Consensus
  • OOD
    • Overview
    • Design Parking
  • Company Tags
    • Meta
    • Citadel
      • C++ Fundamentals
      • 面经1
      • Fibonacci
      • Pi
      • Probability
    • DoorDash
      • Similar String Groups
      • Door And Gates
      • Max Job Profit
      • Design File System
      • Count All Valid Pickup
      • Most Profit Assigning Work
      • Swap
      • Binary Tree Max Path Sum
      • Nearest Cities
      • Exployee Free Time
      • Tree Add Removal
    • Lyft
      • Autocomplete
      • Job Scheduler
      • Read4
      • Kvstore
    • Amazon
      • Min Binary Str Val
    • AppLovin
      • TODO
      • Java Basic Questions
    • Google
      • Huffman Tree
      • Unique Elements
    • Instacart
      • Meeting Rooms II
      • Pw
      • Pw2
      • Pw3
      • Expression1
      • Expression2
      • Expression3
      • PW All
      • Expression All
      • Wildcard
      • Free forum tech discussion
    • OpenAI
      • Spreadsheet
      • Iterator
      • Kv Store
    • Rabbit
      • Scheduler
      • SchedulerC++
    • [Microsoft]
      • Min Moves Spread Stones
      • Inorder Successor
      • Largest Palindromic Num
      • Count Unique Char Substrs
      • Reverse Words Ii
      • Find Missing Observations
      • Min Fuel Report Capital
      • Design Hashmap
      • Find Original Array From Doubled
      • Num Flowers Bloom
      • Distribute Coins
      • Find Median From Stream
Powered by GitBook
On this page
  • Functional Requirements
  • Non-functional requirements:
  • Scale
  • API
  • High Level Diagram
  • E2E
  • Data Model
  • Routing Tiles
  • User Location Data
  • Geocoding DB
  • Precomputed Map Tiles
  • Deep Dive
  • Location Service
  • Rendering Map Tiles
  • Navigation Service
  • Adaptive ETA and rerouting
  • TODO
  1. System Design
  2. Mocks

Google Maps

Functional Requirements

  • User Location Update

    • Monitor live traffic, detect new and closed roads, analyze user behavior for personalization.

    • Use real-time data to provide accurate ETA and reroute options.

    • Leverage stream of location data to improve system.

  • Navigation Service, including ETA

  • Map rendering

Non-functional requirements:

  • Accuracy

  • Low latency

  • Highly available

Scale

1B DAU

35 minutes per week for avg user

1B * 35 / 7 = 5B minutes per day.

QPS:

  1. Navigation Requests

twice a day -> 2B requests a day -> 2*10^9 / 10^5 = 20000 QPS

  1. Location Update Requests

300B requests per day if send requests every seconds.

300B per day = 3M QPS

if send requests every 15 seconds = 3M / 15 = 30*10^5 / 15 = 200000 QPS.

Peak traffic = 2*10^5 * 5 = 1M QPS

Storage

At zoom level 21, about 4.3 trillion map tiles.

Each tile is a 256x256 pixel compressed PNG, size = 100KB

4.3 trillion x 100 KB = 440PB

Compressed image + natural lands = 44 to 88PB

100 PB in total

API

Location Update:

POST /v1/locations

Payload: JSON encoded array {latitude, longitude, timestamp}

GET /v1/navigation?origin=xxx&destination=xxx

Response

{
    'distance': {'text': '0.2 mi', 'value': 259},
    'duration': {'text': '1 min', 'value': 83},
    'start_location': {'lat': 37.4027165, 'lng': -121.9435809},
    'end_location': {'lat': 37.4021232, 'lng': xxx},
    'polyline': {'points': 'xxx'},
    'geocoded_waypoints': [
        {
            "geocoder_status": "OK",
            "place_id": "xxx"
        },
        {...},
        {...}
    ],
    'travel_mode': 'DRIVING'
}

High Level Diagram

E2E

App Initialization

  1. Client open up the app, the GPS on the phone will locate user and its coordinate.

  2. Client sends map rendering requests to fetch map tiles CDN urls to render nearby map on the specific zoom level.

  3. Client set up websocket connections to receive reroute options, ETA updates.

Navigation

  1. Client search for some place / address on search bar.

  2. Backend will route request to geocoding service to convert place or address to latitude and longitude pair.

  3. Backend returns the detail information about the place as well as the navigation option response based on user location and destination location.

Location Update

  1. Client send location update requests every 15 seconds.

Data Model

Routing Tiles

By breaking up road networks into routing tiles that can be loaded on demand, the routing algorithms can significantly reduce memory consumption and improve pathfinding performance by only consuming a small subset of the routing tiles at a time, and only loading additional tiles as needed.

Initial dataset contains roads and associated metadata like names, country, longitude and latitude. The data is not organized as graph structure and is not usable by most routing algorithm.

Each tile contains a list of graph nodes and edges representing the intersections and roads.

It's efficient to store it in S3 and cache it progressively. We can use some package/library to serialize adjacency lists into a binary file. We can organize tiles by its geohashes for fast look ups.

Road Segments

A road can be represented as a list of connected points: (lat1, lng1), (lat2, lng2)...(latN, lngN). Calculate the geohash for every point, and identify which tiles the road passes through.

For each road segment that crosses multiple tiles, you can split it into smaller segment lie within individual tiles.

Schema:
segment_id: uuid
start_point: The starting geographical coordinate of the segment.
end_point: The ending coordinate of the segment.
geom: The geometry of the segment, stored as LineString in PostGIS.
geohash: The geohash tile this road belongs to.
road_type: enum, "highway", "local" etc.
speed_limit: xx mph
other_attributes: is_toll_road, surface_type etc.

User Location Data

user_id
timestamp
driving_mode
location

101

1635740977

driving

(20.0, 30.5)

Geocoding DB

Redis, Key-Value

key: place_name, address. value: lat/lng pair.

Precomputed Map Tiles

Store in S3 backed by CDN.

Deep Dive

Location Service

Rendering Map Tiles

WebGL: Instead of sending images over network, we can send vector formations (paths and polygons)

Pros: vector tiles provide a much better zooming experience.

Navigation Service

Shortest Path Service

  1. Receives the origin and destination in lat/lng pairs. Load start points and end points of routing tiles based on geohashes.

  2. Starts with origin routing tile, as it traverse the graph, hydrates neighboring tiles from object storage, including bigger tiles at higher zoom level so that it can make use of highway roads etc.

ETA Service

Use machine learning to predict ETAs based on current traffic and historical data.

Ranker Service

Navigation service obtains the ETA predications, passes the info to ranker to rank possible routes from fastest to slowest, return top-k results to navigation service.

Updater Service

Tap into Kafka location update stream and asynchronously update traffic DB and routing tiles DB.

Update routing tiles DB: responsible for transforming the road dataset with newly found roads and road closures into a updated set of routing tiles.

Update traffic DB: Extracts traffic conditions from the streams of location updates sent by active users. Enable ETA service to provide better estimates.

Adaptive ETA and rerouting

The system needs to track all active navigating users and update them on ETA continuously.

  • How do we track actively navigating users?

  • How do we store data, so we efficiently locate the users affected by traffic changes among millions of navigation routes?

Traffic DB stores actively navigating users with routing tile information:

user1: current_r_1, super(r_1), super(super(r_1)), ...

We store the upper zoom level routing tile until we found the destination.

To find if a user is affected by the traffic change, we need only check if a routing tile is inside the last routing tile of the row in record.

We prefer to use websocket to communicate the reroute options to clients.

TODO

PreviousDistributed Message QueueNextNearby Friends

Last updated 1 year ago

Drawing
Drawing
Drawing