Modo entrevista. Cronômetro de 10 min, sem dicas e sem autocompletar
Merge two sorted arrays
MédiaArrays10 min
Write a function mergeArrays(a, b). Both arrays are sorted from small to large. Return a new array with all elements of a and b, also sorted from small to large.
aandbhold integers, also negative ones. Each one can have any length, also0.- Keep repeated values:
[1, 2]and[2, 3]give[1, 2, 2, 3]. - Always return a new array, even when
aorbis empty. Do not change (do not mutate)aorb. - Do not sort the joined array. Walk through both arrays once, like merging two sorted piles of cards. A hidden test makes
sortandtoSortedthrow an error.
Exemplos
mergeArrays([1, 3, 5], [2, 4])Retorna [ 1, 2, 3, 4, 5 ]
mergeArrays([], [7])Retorna [ 7 ]
mergeArrays([1, 2], [2, 3])Retorna [ 1, 2, 2, 3 ]
Cobrada em Mastercard · Fonte
Carregando o editor...
- Entrada
mergeArrays([ 1, 3, 5 ], [ 2, 4 ])Esperado[ 1, 2, 3, 4, 5 ] - Entrada
mergeArrays([], [ 7 ])Esperado[ 7 ] - Entrada
mergeArrays([ 1, 2 ], [ 2, 3 ])Esperado[ 1, 2, 2, 3 ]
Os testes ocultos rodam no envio