Guest User

Untitled

a guest
Jan 16th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. /*
  2. Good morning. Here's your coding interview problem for today.
  3. This problem was asked by Google.
  4.  
  5. Given a stack of N elements, interleave the first half of the stack with the second half reversed using only one other queue. This should be done in-place.
  6.  
  7. Recall that you can only push or pop from a stack, and enqueue or dequeue from a queue.
  8.  
  9. For example, if the stack is [1, 2, 3, 4, 5], it should become [1, 5, 2, 4, 3]. If the stack is [1, 2, 3, 4], it should become [1, 4, 2, 3].
  10. */
  11.  
  12. var stack = [1, 2, 3, 'a', 'b', 'c'];
  13. var queue = [];
  14.  
  15. for(var i = 0; i < (stack.length / 2); i++) {
  16. var rightIndex = (stack.length - i) -1;
  17.  
  18. queue.push(stack[i]);
  19.  
  20. if (i !== rightIndex) {
  21. queue.push(stack[rightIndex]);
  22. }
  23. }
  24.  
  25. // [1, 'c', 2, 'b', 3, 'a']
Add Comment
Please, Sign In to add comment