DEV Community

Discussion on: Interview Qs Decoded - # 2

Collapse
 
miketalbot profile image
Mike Talbot ⭐ • Edited

It's an interesting problem because it drives for a more optimal solution rather than desiring some super terse response I think. Though clearly .reverse() is highly optimised and terse! On DEV we often see solutions to problems written deliberately functionally (I do this a lot). Asking for no .reduce() definitely pushes us to realise we are being asked for an actual function not some kind of super expression and we should therefore be able to strive for optimal performance and memory use.

I run a lot of interviews, in this question I'd be looking for the following in the answer:

  • Don't use string appending - if the string was long this is a very slow solution - the result should concatenate an output array using .join()
  • Don't build an array using the spread operator at each step, it's even worse than string appending for performance and memory usage/garbage collection.
  • An optimal solution will cache commonly dereferenced properties to improve performance

Along with another commenter I also like your use of pop() very smart that. I think there's a slight error in it though - surely your function adds one too many characters? <=str.length - overall your solution is great.

Minimised space usage would have me making this hybrid of solutions mentioned:

function reverseString(str) {
     const source = str.split('')
     const swap = source.length >> 1
     for(let i = 0, j = source.length-1; i < swap;i++,j--) {
         let a = source[i]
         source[i] = source[j]
         source[j] = a
     }
     return source.join('')
}

Enter fullscreen mode Exit fullscreen mode
  • Writing out that variable swap rather than using [source[i], source[j]] = [source[j], source[i]] avoids creating 2 arrays on every loop. These would be quickly garbage collected but it depends on the browser as to just how much memory/time that uses.
  • This has only one array being used and so reduces allocations
Collapse
 
cat profile image
Cat

I actually used splitString.length at first during the interview, but I wasn't getting the right result-- I came up short. Then I realized I needed to use str.length instead to get the desired result, and it was what the interviewer was looking for.

Thank you for your well-thought-out, detailed comment! I will keep those pointers in mind during the next technical interview.

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Ah well I'd have made that mistake as well then :) Fair enough!

Collapse
 
johannesjo profile image
Johannes Millan • Edited

Performance is one way to look at it, another is readability and ease of use. If you assume only reasonably short strings and a reasonable amount of iterations then I would argue that the easier readable solution is the better one, as it is inviting less misunderstandings and saves time every time a developer reads your code. Just mentioning that so you don't judge your interviewees prematurely only on performance ;)