CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

πŸ”„ Array.prototype.reverse() Polyfill

The reverse() method reverses an array in place and returns it. Efficiently swaps elements from both ends toward the center.

The method reverses an array in place and returns the reference to the same array. -- βœ… Implementation Key Features: In-place Reversal : The array is reversed directly, modifying the original array. Two-pointer Approach : Efficient swapping using two pointers ( and ) that move towards each other. Time Complexity : β€”each element is swapped once. Space Complexity : β€”no additional memory is used, other than the variables for the pointers. This method efficiently reverses the array while mimicking the behavior of the native method in JavaScript. -- <!-quiz-start --Q1: Does modify the original array or return a new one? [x] Modifies the original array in place [ ] Returns a new reversed array [ ] Depends on the array length [ ] Creates a shallow copy first Q2: What is the time complexity of the two-pointer reverse algorithm? [ ] O(n^2) [x] O(n) [ ] O(log n) [ ] O(1) Q3: What is the space complexity of the in-place reverse polyfill? [x] O(1) [ ] O(n) [ ] O(log n) [ ] O(n^2) <!-quiz-end --
JavaScriptPolyfills
🎯 Array.prototype.at() Polyfill
medium
βœ… Array.prototype.every() Polyfill
medium
πŸ”² Array.prototype.fill() Polyfill
medium
πŸ” Array.prototype.filter() Polyfill
medium
πŸ”Ž Array.prototype.find() Polyfill
medium
πŸ”’ Array.prototype.findIndex() Polyfill
medium
πŸ”™ Array.prototype.findLast() Polyfill
medium
πŸ”™ Array.prototype.findLastIndex() Polyfill
medium
πŸ“‹ Array.prototype.flat() Polyfill
medium
πŸ” Array.prototype.includes() Polyfill
medium
πŸ”’ Array.prototype.indexOf() Polyfill
medium
βœ… Array.isArray() Polyfill
medium
πŸ—ΊοΈ Array.prototype.map() Polyfill
hard
βž– Array.prototype.pop() Polyfill
medium
βž• Array.prototype.push() Polyfill
easy
πŸ”„ Array.prototype.reduce() Polyfill
medium
πŸ”„ Array.prototype.reverse() Polyfill
hard
⬅️ Array.prototype.shift() Polyfill
hard
πŸ”˜ Array.prototype.some() Polyfill
medium
πŸ”€ Array.prototype.sort() Polyfill
hard
➑️ Array.prototype.unshift() Polyfill
hard
πŸ“ž Function.prototype.apply() Polyfill
medium
πŸ”— Function.prototype.bind() Polyfill
hard
πŸ“ž Function.prototype.call() Polyfill
medium
17 of 24
LibraryJavaScriptPolyfills32 of 61

πŸ”„ Array.prototype.reverse() Polyfill

jspolyfillshard

The reverse() method reverses an array in place and returns the reference to the same array.


βœ… Implementation

Array.prototype.customReverse = function() {
    let left = 0;
    let right = this.length - 1;

    // Swap elements from both ends towards the center
    while (left < right) {
        [this[left], this[right]] = [this[right], this[left]]; // Swap
        left++;
        right--;
    }

    return this; // Return the modified array (reverse is in-place)
};

// Example usage:
const array = ['one', 'two', 'three'];
console.log('array:', array); // Output: ["one", "two", "three"]

const reversed = array.customReverse();
console.log('reversed:', reversed); // Output: ["three", "two", "one"]

// The original array is also reversed since the method modifies it in place
// console.log('array:', array); // Output: ["three", "two", "one"]

Key Features:

  • In-place Reversal : The array is reversed directly, modifying the original array.
  • Two-pointer Approach : Efficient swapping using two pointers (left and right) that move towards each other.
  • Time Complexity : O(n)β€”each element is swapped once.
  • Space Complexity : O(1)β€”no additional memory is used, other than the variables for the pointers.

This method efficiently reverses the array while mimicking the behavior of the native .reverse() method in JavaScript.


Quick Quiz

Test your understanding with 3 quick questions

Q1Does `reverse()` modify the original array or return a new one?
Q2What is the time complexity of the two-pointer reverse algorithm?
Q3What is the space complexity of the in-place reverse polyfill?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna