Snail Traversal - Problem
Snail Traversal Matrix Transformation

You need to enhance JavaScript arrays with a special method called snail(rowsCount, colsCount) that transforms a 1D array into a 2D matrix following a unique "snail traversal" pattern.

๐ŸŒ How Snail Traversal Works:
โ€ข Start at the top-left corner with the first array element
โ€ข Fill the entire first column from top to bottom
โ€ข Move to the second column and fill from bottom to top
โ€ข Continue alternating direction for each column until all elements are placed

Example: Array [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15] with rowsCount = 5, colsCount = 4 becomes:
19  8  1  6
10  5  17 13
3   2  16 11
7   1  14 20
9   17 12 4
    โ†“  โ†‘  15

Invalid Input: Return an empty array [] if rowsCount ร— colsCount โ‰  array.length

Input & Output

example_1.js โ€” Basic 3ร—4 Matrix
$ Input: arr = [1,2,3,4,5,6,7,8,9,10,11,12], rowsCount = 3, colsCount = 4
โ€บ Output: [[1,6,7,12],[2,5,8,11],[3,4,9,10]]
๐Ÿ’ก Note: Column 0: [1,2,3] (top-to-bottom), Column 1: [6,5,4] (bottom-to-top), Column 2: [7,8,9] (top-to-bottom), Column 3: [12,11,10] (bottom-to-top)
example_2.js โ€” Single Row
$ Input: arr = [1,2,3,4], rowsCount = 1, colsCount = 4
โ€บ Output: [[1,2,3,4]]
๐Ÿ’ก Note: With only one row, all elements are placed in the same row, alternating columns but same position
example_3.js โ€” Invalid Dimensions
$ Input: arr = [1,2,3], rowsCount = 3, colsCount = 2
โ€บ Output: []
๐Ÿ’ก Note: 3 ร— 2 = 6, but array length is 3. Since 6 โ‰  3, return empty array

Visualization

Tap to expand
๐ŸŒ Snail Traversal Animation123456789Col 0: โ†“Col 1: โ†‘Col 2: โ†“Input: [1,2,3,4,5,6,7,8,9] โ†’ 3ร—3 MatrixPattern: Down โ†’ Up โ†’ Down โ†’ Up...
Understanding the Visualization
1
Start at Top-Left
Begin placing elements in the first column from top to bottom
2
Alternate Directions
Move to next column and fill from bottom to top
3
Continue Pattern
Keep alternating direction for each subsequent column
4
Complete Matrix
All elements are placed following the snail traversal pattern
Key Takeaway
๐ŸŽฏ Key Insight: Use modular arithmetic to determine column direction - even columns (0,2,4...) go top-to-bottom, odd columns (1,3,5...) go bottom-to-top.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through input array with constant time position calculations

n
2n
โœ“ Linear Growth
Space Complexity
O(rowsCount ร— colsCount)

Space needed to store the output 2D matrix

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค arr.length โ‰ค 250
  • 1 โ‰ค rowsCount, colsCount โ‰ค 250
  • arr contains distinct integers from 1 to arr.length
  • Must implement as Array.prototype.snail method
Asked in
Google 25 Meta 18 Amazon 15 Microsoft 12
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen