Skip to content

javierfrancisco/js-code-interview

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ JavaScript Code Interview Practice

A comprehensive collection of JavaScript algorithmic problems and their implementations, featuring multiple solution approaches for comparison and learning.

JavaScript Node.js Jest License

πŸ“‹ Project Overview

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.

πŸ—οΈ Code Architecture

Directory Structure

.
β”œβ”€β”€ 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

🎯 Algorithm Categories

Two Sum Problem

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

Array Merging

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

Additional Algorithms

  • 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

🚦 Getting Started

Prerequisites

  • Node.js 14+
  • npm or yarn

Installation

# 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

πŸ’» Development Workflow

Running Code

# 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

Module System

  • βœ… ES6 modules with import/export syntax
  • βœ… Each algorithm exports its main function
  • βœ… Centralized exports via src/index.js
  • βœ… Test demonstrations in arrays/index.js

πŸ“ Code Patterns

Naming Conventions

  • Functions: Descriptive camelCase matching the problem domain
  • Files: kebab-case for directories, camelCase for files
  • Imports: Aliased imports for multiple implementations

Implementation Style

/**
 * 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;
}

πŸ§ͺ Testing

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 List

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

🎯 Usage Examples

Two Sum Problem

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]

Array Merging

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]

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-algorithm)
  3. Implement algorithm with tests
  4. Ensure all tests pass (npm test)
  5. Commit your changes (git commit -m 'Add amazing algorithm')
  6. Push to the branch (git push origin feature/amazing-algorithm)
  7. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Algorithm concepts from common interview preparation resources
  • Test patterns inspired by Jest best practices
  • ES6 module structure for modern JavaScript development

πŸ“ž Contact

GitHub: @javierfrancisco


Built with ❀️ for JavaScript interview preparation

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published