Frontend School

Calculate Total Itinerary Segments

DSA
easy

Calculate Total Itinerary Segments

Easy
Recursion
Arrays
Depth-First Search

Overview

At Ixigo, we help millions of users plan their travel with seamless itineraries. These itineraries can range from simple point-to-point journeys to complex multi-city or multi-country tours. To better understand the complexity and scope of a user's travel plan, we sometimes need to quantify the total number of individual 'segments' or 'points of interest' within an itinerary. An itinerary can be a single segment (represented by a numeric value, e.g., a travel duration or cost) or a composite itinerary containing a list of other itineraries, which themselves can be simple segments or further nested composite itineraries.

What you need to do / Task

Your task is to implement a function calculateTotalSegments that takes a nested array representing a travel itinerary and returns the sum of all individual numeric segments found within it. The structure can be arbitrarily nested.

Input & output

Function Signature:

type ItineraryItem = number | ItineraryItem[];

function calculateTotalSegments(itinerary: ItineraryItem[]): number
  • itinerary: A list where each element can either be a number (representing a single travel segment) or another list (representing a composite itinerary containing further ItineraryItems). Each number represents a value associated with that segment (e.g., points, duration, cost).
  • Returns: A number representing the sum of all individual numeric segments across the entire nested structure.

Clarifications

  • The input itinerary will always be an array at the top level.
  • All numeric segments will be positive integers.
  • An empty list within the itinerary (e.g., []) should contribute 0 to the total sum.

Examples

1

Example 1

Input:

itinerary = [1, 2, 3]

Output:

6

Note:

The itinerary contains three simple segments: 1, 2, and 3. Their sum is 1 + 2 + 3 = 6.

2

Example 2

Input:

itinerary = [[1, 2], [3, [4, 5]]]

Output:

15

Note:

The itinerary sums up segments: 1 + 2 (from the first sub-list) + 3 (from the second sub-list) + 4 + 5 (from the nested sub-list) = 15.

3

Example 3

Input:

itinerary = [10, [], [20, [30, []]], 5]

Output:

65

Note:

Segments are 10, 20, 30, and 5. Empty lists contribute 0 to the sum. Total sum is 10 + 20 + 30 + 5 = 65.

Constraints

Constraints & assumptions

  • The input itinerary will only contain number types (positive integers) or array types.
  • The maximum nesting depth of the itinerary is 10.
  • The total number of individual numeric segments will not exceed 1000.
  • The sum of all segments will fit within a standard 32-bit integer.

Out of scope

  • Error handling for invalid input types (e.g., strings, booleans, null/undefined in the arrays).
  • Performance optimization beyond a straightforward recursive traversal.

What interviewers evaluate

  • Correctness in handling the base case of recursion (individual numeric segments).
  • Accuracy in traversing and summing elements within nested array structures.
  • Ability to manage recursion depth and handle potentially empty nested lists.
  • Overall code clarity, structure, and readability.

Loading editor...