An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
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:
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
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To