Manafall Daily Quest

Matrix Spiral Traversal

Function Name: spiralTraversal

Description

Write a function that takes a two-dimensional array (matrix) of MxN elements and returns an array of all elements in spiral order.

The function should start at the top-left corner and proceed in a spiral pattern until every element has been visited, moving in the order of right, down, left, and up sides of the matrix's boundary.

Consider that the input matrix can be of any size, but it is guaranteed to be non-empty.

Requirements

  • The function spiralTraversal must take one parameter - a two-dimensional array (matrix).
  • The function should not modify the input matrix.
  • The function must return a flat array containing elements in the order they were visited during the spiral traversal.
  • Optimize the function for readability and efficiency.

Examples

Given the following 2D array (matrix): [[1, 2, 3], [4, 5, 6], [7, 8, 9]], the function spiralTraversal should return [1, 2, 3, 6, 9, 8, 7, 4, 5].For a matrix [[1, 2, 3, 4]], the function should return [1, 2, 3, 4].For a matrix [[1], [2], [3], [4]], the function should return [1, 2, 3, 4].

Links

https://en.wikipedia.org/wiki/Spiralhttps://www.mathsisfun.com/algebra/matrix-introduction.html

Minimal Swaps to Ascend

Sun Nov 10 2024

Write a function that calculates the minimum number of swaps required to sort an array of unique integers in ascending order. The function should perform this calculation without actually sorting the array.Each swap can only exchange two elements. Assume that the input array does not contain any duplicate values and contains at least two numbers.

Prev Quest

Merge Intervals

Tue Nov 12 2024

Write a function called mergeIntervals that takes an array of intervals where each interval is a pair of integers indicating the start and end of a time block. The input array may not be sorted and could have overlapping intervals. The function should merge overlapping intervals and should return an array of the non-overlapping intervals that cover all the intervals in the input.An interval is considered to be overlapping if it shares any common time with another interval. The start and end times are inclusive.The intervals should be returned in sorted order by their start times.

Next Quest