Tokenize a 3D trajectory into movement tokens and predict the next one

Quick Overview

Convert a sequence of 3D points into three-letter tokens that record whether each axis moved forward, moved backward or stayed still, then append the most frequent token as a prediction. The problem tests sign handling, frequency counting and a first-occurrence tie-break.

Tokenize a 3D trajectory into movement tokens and predict the next one

Company: Waymo

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a list of 3D trajectory points representing a vehicle's path, where each point is `[x, y, z]`, tokenize the trajectory into discrete movement tokens. Each token represents the direction of movement between consecutive points along each axis: `'F'` (forward, a positive change), `'B'` (backward, a negative change), or `'S'` (stationary, zero change). A token is a string of exactly three characters giving the movement along the `x`, `y` and `z` axes, in that order. After computing all movement tokens, predict the next step by appending the most frequent token to the end of the token list. If there is a tie in frequency, choose the token that appeared first in the sequence. Return the list of all tokens, including the predicted next token at the end. ### Function Signature ```python def tokenize_trajectory(points: list[list[int]]) -> list[str]: ``` ### Rules - The change on an axis is the next point's coordinate minus the current point's coordinate. - The result holds one movement token per pair of consecutive points, in order, followed by the predicted token, so its length equals `len(points)`. - Among tokens tied for the highest count, the one whose first occurrence has the smallest index wins. ### Constraints - `2 <= len(points) <= 10^4` - `-10^6 <= points[i][j] <= 10^6` - Each point has exactly 3 coordinates `[x, y, z]`. ### Examples ```text Input: points = [[0, 0, 0], [1, 2, -1], [2, 2, 0], [3, 1, 0]] Output: ["FFB", "FSF", "FBS", "FFB"] ``` The three steps are `(1, 2, -1)`, `(1, 0, 1)` and `(1, -1, 0)`, which give "FFB", "FSF" and "FBS". Each token appears once, and "FFB" appeared first, so it is the prediction. ```text Input: points = [[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3], [2, 2, 2]] Output: ["FFF", "FFF", "FFF", "BBB", "FFF"] ``` The first three steps move forward on every axis ("FFF") and the last moves backward on every axis ("BBB"). "FFF" appears 3 times against once for "BBB", so it is the prediction. ```text Input: points = [[0, 0, 0], [0, 0, 0], [1, 0, 0], [1, 0, -5], [3, 0, -5], [3, 0, -7]] Output: ["SSS", "FSS", "SSB", "FSS", "SSB", "FSS"] ``` The steps are `(0, 0, 0)`, `(1, 0, 0)`, `(0, 0, -5)`, `(2, 0, 0)` and `(0, 0, -2)`. "FSS" and "SSB" both appear twice. "FSS" first appears at index 1 and "SSB" at index 2, so "FSS" is the prediction, even though the very first token is "SSS".

Overview: Convert a sequence of 3D points into three-letter tokens that record whether each axis moved forward, moved backward or stayed still, then append the most frequent token as a prediction. The problem tests sign handling, frequency counting and a first-occurrence tie-break.

Waymo logo
Waymo
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a list of 3D trajectory points representing a vehicle's path, where each point is [x, y, z], tokenize the trajectory into discrete movement tokens.

Each token represents the direction of movement between consecutive points along each axis: 'F' (forward, a positive change), 'B' (backward, a negative change), or 'S' (stationary, zero change). A token is a string of exactly three characters giving the movement along the x, y and z axes, in that order.

After computing all movement tokens, predict the next step by appending the most frequent token to the end of the token list. If there is a tie in frequency, choose the token that appeared first in the sequence.

Return the list of all tokens, including the predicted next token at the end.

Function Signature

def tokenize_trajectory(points: list[list[int]]) -> list[str]:

Rules

  • The change on an axis is the next point's coordinate minus the current point's coordinate.
  • The result holds one movement token per pair of consecutive points, in order, followed by the predicted token, so its length equals len(points) .
  • Among tokens tied for the highest count, the one whose first occurrence has the smallest index wins.

Constraints

  • 2 <= len(points) <= 10^4
  • -10^6 <= points[i][j] <= 10^6
  • Each point has exactly 3 coordinates [x, y, z] .

Examples

Input: points = [[0, 0, 0], [1, 2, -1], [2, 2, 0], [3, 1, 0]]
Output: ["FFB", "FSF", "FBS", "FFB"]

The three steps are (1, 2, -1), (1, 0, 1) and (1, -1, 0), which give "FFB", "FSF" and "FBS". Each token appears once, and "FFB" appeared first, so it is the prediction.

Input: points = [[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3], [2, 2, 2]]
Output: ["FFF", "FFF", "FFF", "BBB", "FFF"]

The first three steps move forward on every axis ("FFF") and the last moves backward on every axis ("BBB"). "FFF" appears 3 times against once for "BBB", so it is the prediction.

Input: points = [[0, 0, 0], [0, 0, 0], [1, 0, 0], [1, 0, -5], [3, 0, -5], [3, 0, -7]]
Output: ["SSS", "FSS", "SSB", "FSS", "SSB", "FSS"]

The steps are (0, 0, 0), (1, 0, 0), (0, 0, -5), (2, 0, 0) and (0, 0, -2). "FSS" and "SSB" both appear twice. "FSS" first appears at index 1 and "SSB" at index 2, so "FSS" is the prediction, even though the very first token is "SSS".