Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 11, 2020 11:40 pm GMT

Interview Qs Decoded - 2

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!";

Setup:

function revFunction(str){};

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('');};

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++){}};

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();      // --------   }};

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());      // --------   }};

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

// [!,e,m, ,e,s,r,e,v,e,R]

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('');   // ---- //};

This should return:

//!em esreveR

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!


Original Link: https://dev.to/catcarbn/interview-qs-decoded-2-5eao

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To