Skip to the code
cf.
+10 XP

#98 Reverse a Breadcrumb Trail

easy

A navigation component stores the breadcrumb trail as an array of page names, from the home page to the current page. A new "back to start" view needs them in the opposite order.

Write reverseTrail(crumbs) that reverses crumbs in place, without creating a second array and without calling reverse. The tests check crumbs after your function runs.

Example 1

Input: crumbs = ["Home","Shop","Shoes","Running"]
Output: ["Running","Shoes","Shop","Home"]

The last crumb becomes the first, and so on.

Example 2

Input: crumbs = ["Home","Docs","API"]
Output: ["API","Docs","Home"]

With an odd count, the middle crumb Docs stays put.

Constraints

  • 0 <= crumbs.length <= 100000
  • Each item is a non-empty string.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).