Interview Qs Decoded - # 2

Cat - Jun 11 '20 - - Dev Community

I wanted to share with you all an actual challenge I was given during an interview!

The interviewer said, "Alright, let's do some string reversals!"

I responded with this exact face:
A very excited girl


The Q: Write a function that accepts a string as a single argument, and when called, reverses the string.

Restrictions:

  • You cannot use .reverse() or .reduce()!

Edge Cases:

  • Punctuation does not matter
  • Disregard numbers, we are only looking at strings

Given Argument:

const string="Reverse me!";
Enter fullscreen mode Exit fullscreen mode

Setup:

function revFunction(str){
};
Enter fullscreen mode Exit fullscreen mode

I asked the interviewer if I could use .split(), and he said, "sure, go ahead."

Protip: Always ask questions!!

function revFunction(str){
   const splitString =  str.split('');
};
Enter fullscreen mode Exit fullscreen mode

What .split() does is separate each character into individual indexes, making it into an array. I set it as a const variable splitString so I could call on it later.

Next, I thought of iterating through this new array, and so I utilized a classic for-loop

function revFunction(str){
   const splitString =  str.split('');
   for(let i = 0; i <= str.length; i++){}
};
Enter fullscreen mode Exit fullscreen mode

Now, what do I do when iterating through str? Yes, I need to go through them individually, but I need to iterate the other way. I asked if I could use .pop() since pop removes the last item in an array. He gave me the go-ahead.

function revFunction(str){
   const splitString =  str.split('');
   for(let i = 0; i <= str.length; i++){
      // --------
      splitString.pop();
      // --------
   }
};
Enter fullscreen mode Exit fullscreen mode

But we need that last letter! We can't just send it into the abyss! My solution to hold the popped item was to .push() it into a new, empty array.

function revFunction(str){
   const splitString =  str.split('');
   // --------
   const reversedString = [];
   // --------
   for(let i = 0; i <= str.length; i++){
      // --------
       reversedString.push(splitString.pop());
      // --------
   }
};
Enter fullscreen mode Exit fullscreen mode

At this point, if we console.log the revFunction, it will return as an array.

// [!,e,m, ,e,s,r,e,v,e,R]
Enter fullscreen mode Exit fullscreen mode

Success!

But, the interviewer wanted the function to return a solid string. I asked if I could use .join(). He approved.

function revFunction(str){
   const splitString =  str.split('');
   const reversedString = [];
   for(let i = 0; i <= str.length; i++){
       reversedString.push(splitString.pop());
   }
   // ---- //
   return reversedString.join('');
   // ---- //
};
Enter fullscreen mode Exit fullscreen mode

This should return:

//!em esreveR
Enter fullscreen mode Exit fullscreen mode

We did it! There are many ways to reverse a string without the .reverse() method. What I covered was the iterative way (iterating with a for-loop).

I challenge you to solve this challenge (in your language of choice) in the comments!


Try it out on my Repl!


Thanks for reading!!

Got a beginner-level interview question for me? DM me on Twitter! I'm always up for challenges!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .