Write a function that takes
an array of characters and reverses the
letters in place.
Why an array of
characters instead of a string?
The goal of this
question is to practice manipulating strings
in place. Since we're modifying the
input, we need
a mutable
type
like an array,
instead
of JavaScript's
immutable strings.
In general, an in-place algorithm will require swapping elements.
We swap the first and last characters, then the second and second-to-last characters, and so on until we reach the middle.
function reverse(arrayOfChars) {
let leftIndex = 0;
let rightIndex = arrayOfChars.length - 1;
while (leftIndex < rightIndex) {
// Swap characters
const temp = arrayOfChars[leftIndex];
arrayOfChars[leftIndex] = arrayOfChars[rightIndex];
arrayOfChars[rightIndex] = temp;
// Move towards middle
leftIndex++;
rightIndex--;
}
}
Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?
It's easy and quick. No "reset password" flow. No password to forget.
It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
It makes it harder for one person to share a paid Interview Cake account with multiple people.
“Interview Cake has been a great resource for me! The weekly questions (and more importantly, the well-explained answers) have prepared me way more than anything else I've done for my interviews. Keep doing what you're doing!
—
Zach