A comprehensive collection of JavaScript algorithmic problems and their implementations, featuring multiple solution approaches for comparison and learning.
This repository contains common algorithmic problems frequently asked in JavaScript coding interviews. Each problem includes multiple implementation approaches to showcase different time/space trade-offs and coding patterns.
.
βββ main.js # Entry point with simple array filtering
βββ arrays/ # Legacy array algorithm demonstrations
β βββ index.js # Demo runner for array algorithms
βββ src/
β βββ index.js # Main export hub for all algorithms
β βββ algorithms/ # Algorithm implementations
β βββ two-sum/ # Two Sum problem variants
β βββ merge-arrays/ # Array merging approaches
β βββ ... # Other algorithms
βββ tests/ # Jest test suites
βββ algorithms/ # Algorithm-specific tests
Finding pairs of numbers that sum to a target value
| Implementation | Time Complexity | Space Complexity | File |
|---|---|---|---|
| Hash Table | O(n) | O(n) | src/algorithms/two-sum/hashTable.js |
| Two Pointers | O(n log n) | O(1) | src/algorithms/two-sum/twoPointers.js |
Combining two sorted arrays while maintaining order
| Implementation | Approach | File |
|---|---|---|
| Separate Array | Two-pointer merge with new array | src/algorithms/merge-arrays/separate.js |
| Optimized | Enhanced edge case handling | src/algorithms/merge-arrays/optimized.js |
| In-Place | Splice operations for memory efficiency | src/algorithms/merge-arrays/inPlace.js |
- Product Except Self - Calculate product array excluding current element
- Remove Even Numbers - Filter arrays for odd values only
- Rearrange Positive/Negative - Partition arrays by sign
- Find Minimum Value - Locate smallest element
- Rotate Array - Circular array rotation
- Node.js 14+
- npm or yarn
# Clone the repository
git clone https://github.com/javierfrancisco/js-code-interview.git
# Navigate to project directory
cd js-code-interview
# Install dependencies
npm install# Run main entry point
node main.js
# Run array algorithm demonstrations
node arrays/index.js
# Run specific algorithm file
node -e "import('./src/algorithms/two-sum/hashTable.js')"
# Run all tests
npm test
# Run specific test suite
npm test -- tests/algorithms/two-sum.test.js- β
ES6 modules with
import/exportsyntax - β Each algorithm exports its main function
- β
Centralized exports via
src/index.js - β
Test demonstrations in
arrays/index.js
- Functions: Descriptive camelCase matching the problem domain
- Files: kebab-case for directories, camelCase for files
- Imports: Aliased imports for multiple implementations
/**
* Problem description and constraints
* @param {type} param - Parameter description
* @returns {type} Return value description
*/
function algorithmName(param) {
// Time: O(n), Space: O(1)
// Implementation with inline comments
// explaining the approach
return result;
}All algorithms include comprehensive Jest test suites:
# Run all tests with coverage
npm test -- --coverage
# Run tests in watch mode
npm test -- --watch
# Run specific test pattern
npm test -- --testNamePattern="Two Sum"| Algorithm | Category | Complexity | Status |
|---|---|---|---|
| Two Sum (Hash) | Arrays | O(n) | β Tested |
| Two Sum (Pointers) | Arrays | O(n log n) | β Tested |
| Merge Sorted Arrays | Arrays | O(n+m) | β Tested |
| Product Except Self | Arrays | O(n) | β Tested |
| Remove Even Numbers | Arrays | O(n) | β Tested |
| Rearrange by Sign | Arrays | O(n) | β Tested |
| Find Minimum | Search | O(n) | π§ Pending |
| Find Second Max | Search | O(n) | π§ Pending |
| First Non-Repeating | Hash | O(n) | π§ Pending |
| Rotate Array | Arrays | O(n) | π§ Pending |
import { twoSumHashTable, twoSumTwoPointers } from './src/index.js';
// Hash table approach - best for unsorted arrays
const result1 = twoSumHashTable([2, 7, 11, 15], 9);
console.log(result1); // [2, 7]
// Two pointers - best for sorted arrays
const result2 = twoSumTwoPointers([2, 7, 11, 15], 9);
console.log(result2); // [2, 7]import { mergeTwoSortedArrays } from './src/index.js';
const arr1 = [1, 3, 5];
const arr2 = [2, 4, 6];
const merged = mergeTwoSortedArrays(arr1, arr2);
console.log(merged); // [1, 2, 3, 4, 5, 6]- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-algorithm) - Implement algorithm with tests
- Ensure all tests pass (
npm test) - Commit your changes (
git commit -m 'Add amazing algorithm') - Push to the branch (
git push origin feature/amazing-algorithm) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Algorithm concepts from common interview preparation resources
- Test patterns inspired by Jest best practices
- ES6 module structure for modern JavaScript development
GitHub: @javierfrancisco
Built with β€οΈ for JavaScript interview preparation