An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).
The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.
Write a function:
function solution(A, K);
that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.
For example, given
A = [3, 8, 9, 7, 6] K = 3the function should return [9, 7, 6, 3, 8]. Three rotations were made:
[3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7] [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9] [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]For another example, given
A = [0, 0, 0] K = 1the function should return [0, 0, 0]
Given
A = [1, 2, 3, 4] K = 4the function should return [1, 2, 3, 4]
Assume that:
- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, K) {
// write your code in JavaScript (Node.js 8.9.4)
if(A.length <= 1){
return A;
}
for(let i =0; i < K; i++) {
let element = A.shift();
array.unshift(elemnt)
}
}
solution.js:9 let element[] = A.shift(); ^ SyntaxError: Unexpected token [ at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:401:29) at Promise.resolve.then (/tmp/exec.js:435:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
solution.js:9 let element[] = A.shift(); ^ SyntaxError: Unexpected token [ at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:401:29) at Promise.resolve.then (/tmp/exec.js:435:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
solution.js:9 let element[] = A.shift(); ^ SyntaxError: Unexpected token [ at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:401:29) at Promise.resolve.then (/tmp/exec.js:435:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, K) {
// write your code in JavaScript (Node.js 8.9.4)
let arr = [];
for(let i =0; i < K; i++) {
const element = A.shift();
arr
}
A.unshift(element);
return A;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, K) {
// write your code in JavaScript (Node.js 8.9.4)
let arr = [];
for(let i =0; i < K; i++) {
const element = A.shift();
arr.push()
}
A.unshift(element);
return A;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, K) {
// write your code in JavaScript (Node.js 8.9.4)
let arr = [];
for(let i =0; i < K; i++) {
const element = A.shift();
arr.push(element);
}
A.unshift(arr);
return A;
}
Invalid result type, integer expected, 'object' found
Invalid result type, integer expected, 'object' found
Invalid result type, integer expected, 'object' found
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 7, 6 ]
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 0, 0 ]
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[]
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 7, 6 ]
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 0, 0 ]
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[]
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 7, 6 ]
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 0, 0 ]
Invalid result type, Array expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[]
[ 8, 9, 7, 6 ] [ 9, 7, 6 ] [ 7, 6 ]
[ 0, 0 ]
[ 2, 3, 4 ] [ 3, 4 ] [ 4 ] []
[ 3, 8, 9, 7, 6 ] [ 3, 8, 9, 7, 6 ] [ 3, 8, 9, 7, 6 ]
[ 0, 0, 0 ]
[ 1, 2, 3, 4 ] [ 1, 2, 3, 4 ] [ 1, 2, 3, 4 ] [ 1, 2, 3, 4 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, K) {
// write your code in JavaScript (Node.js 8.9.4)
let arr = [];
for(let i =0; i < K; i++) {
const ele = A.shift();
A.unshift(A[i]);
console.log(A);
}
return A;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, K) {
// write your code in JavaScript (Node.js 8.9.4)
let arr = [];
for(let i =0; i < K; i++) {
const ele = A.shift();
A.unshift(ele]);
console.log(A);
}
return A;
}
solution.js:10 A.unshift(ele]); ^^^ SyntaxError: missing ) after argument list at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:401:29) at Promise.resolve.then (/tmp/exec.js:435:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
solution.js:10 A.unshift(ele]); ^^^ SyntaxError: missing ) after argument list at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:401:29) at Promise.resolve.then (/tmp/exec.js:435:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
solution.js:10 A.unshift(ele]); ^^^ SyntaxError: missing ) after argument list at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:401:29) at Promise.resolve.then (/tmp/exec.js:435:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, K) {
// write your code in JavaScript (Node.js 8.9.4)
let arr = [];
for(let i =0; i < K; i++) {
const ele = A.shift();
A.unshift(ele);
console.log(A);
}
return A;
}
[ 3, 8, 9, 7, 6 ] [ 3, 8, 9, 7, 6 ] [ 3, 8, 9, 7, 6 ]
[ 0, 0, 0 ]
[ 1, 2, 3, 4 ] [ 1, 2, 3, 4 ] [ 1, 2, 3, 4 ] [ 1, 2, 3, 4 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, K) {
// write your code in JavaScript (Node.js 8.9.4)
let arr = [];
for(let i =0; i < K; i++) {
const ele = A.shift();
//A.unshift(ele);
console.log(ele,A);
}
return A;
}
3 [ 8, 9, 7, 6 ] 8 [ 9, 7, 6 ] 9 [ 7, 6 ]
0 [ 0, 0 ]
1 [ 2, 3, 4 ] 2 [ 3, 4 ] 3 [ 4 ] 4 []
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, K) {
// write your code in JavaScript (Node.js 8.9.4)
let arr = [];
for(let i =0; i < K; i++) {
const ele = A.POP();
//A.unshift(ele);
console.log(ele,A);
}
return A;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, K) {
// write your code in JavaScript (Node.js 8.9.4)
let arr = [];
for(let i =0; i < K; i++) {
const ele = A.pop();
//A.unshift(ele);
console.log(ele,A);
}
return A;
}
6 [ 3, 8, 9, 7 ] 7 [ 3, 8, 9 ] 9 [ 3, 8 ]
0 [ 0, 0 ]
4 [ 1, 2, 3 ] 3 [ 1, 2 ] 2 [ 1 ] 1 []
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, K) {
// write your code in JavaScript (Node.js 8.9.4)
let arr = [];
for(let i =0; i < K; i++) {
const ele = A.pop();
A.unshift(ele);
console.log(ele,A);
}
return A;
}
6 [ 6, 3, 8, 9, 7 ] 7 [ 7, 6, 3, 8, 9 ] 9 [ 9, 7, 6, 3, 8 ]
0 [ 0, 0, 0 ]
4 [ 4, 1, 2, 3 ] 3 [ 3, 4, 1, 2 ] 2 [ 2, 3, 4, 1 ] 1 [ 1, 2, 3, 4 ]
The following issues have been detected: runtime errors.
For example, for the input ([], 1) the solution terminated unexpectedly.
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?