Write a method, sillySum
, that takes in a sequence of digits as a string and returns the sum of all digits that match the next digit in the list. The list is "circular", so the digit after the last digit is the first digit in the list.
Examples:
1122
produces a sum of3
(1
+2
) because the first digit (1
) matches the second digit and the third digit (2
) matches the fourth digit.1111
produces4
because each digit (all1
) matches the next digit.1234
produces0
because no digit matches the next.91212129
produces9
because the only digit that matches the next one is the last digit,9
.
Your solution should run in O(N)
time and take up O(1)
extra space.
function sillySum(digits) {
let sum = 0;
for (let i = 0; i < digits.length; i++) {
if (digits[i] === string[(i + 1) % string.length])
sum += parseInt(string[i]);
}
return sum;
}
You are given a 2D array of random integers. Your goal is to calculate the checksum
of these integers. For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all these differences.
Examples:
[
[5, 1, 9, 5],
[7, 3],
[2, 6, 8]
]
- The first row's largest and smallest values are
9
and1
, and their difference is8
- The second row's largest and smallest values are
7
and3
, and their difference is4
- The third row's difference is
6
- The total
checksum
of this input would be8 + 4 + 6 = 18
This method should take O(N * K)
time, where N is the number if integers in each row, and K is the number of rows. It should take O(1) extra space
function checksum(matrix) {
let totalSum = 0;
matrix.forEach(row => {
let min = row[0], max = row[0];
row.forEach(int => {
if (int < min) min = int;
if (int > max) max = int;
})
totalSum += max - min;
})
return totalSum;
}
Implement a Queue using two Stacks. Assume you already have a Stack
class with the appropriate methods: push
, pop
, peek
, isEmpty
, and size
. Create a class, StackQueue
, that implements this. Your StackQueue
should function through two primary methods, enqueue
and dequeue
. However, feel free to write any additional helper methods that you see fit.
Constraints:
enqueue
:O(1)
dequeue:
O(N)
, butO(1)
amortized
Overview:
- We instantiate our
StackQueue
with two stacks, left and right. - The goal is to always dequeue from the right stack, and always enqueue on the left stack
- If the right stack is empty, that means we have to flip the left stack into the right one
- We can implement a helper method,
flipStacks
, that calls pops every element from the left stack onto the right stack until the left stack is empty
- We can implement a helper method,
class StackQueue {
constructor() {
this.leftStack = new Stack();
this.rightStack = new Stack();
}
flipStacks() {
while (!this.leftStack.isEmpty()) {
this.rightStack.push(this.leftStack.pop());
}
}
enqueue(data) {
this.leftStack.push(data);
}
dequeue() {
if (this.rightStack.isEmpty())
this.flipStacks();
return this.rightStack.pop();
}
}
Takeaway Question:
- What is amortization? Why does the
dequeue
method amortize toO(1)
?
Amortization means that, over time, our
dequeue
method will average out to beO(1)
. This happens because, even thoughflipStacks
isO(N)
, it gives usN
freeO(1)
operations.