Skip to content

This is an open source repo for learning and rehearsing data structures and algorithms in Javascript

License

Notifications You must be signed in to change notification settings

theritikchoure/dsa-javascript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Data Structures and Algorithms (DSA) in TypeScript

GitHub License Typescript

Welcome to the Data Structures and Algorithms (DSA) repository! This repository is dedicated to providing a collection of DSA problems and their solutions in a clear and well-documented format.

For me great algorithms are the poetry of computation. Just like verse, they can be terse, allusive, dense and even mysterious. But once unlocked, they cast a brilliant new light on some aspect of computing.

- Francis Sullivan

Table of Contents

  1. Big O
  2. Different Complexities
  3. Running Tests
  4. Questions
  5. Other Resources
  6. FAQ

Big O

Big O is a mathematical notation commonly used to describe the impact on time or space as input size n increases. We are mostly interested in discussing the worst case of an algorithm, but it is also beneficial to compare algorithms in their average and best case scenarios. Seven Big O notations commonly used in algorithm complexity analysis are discussed in the following sections.

[Figure 1] Schematic diagram of Big O for common run times from fastest to slowest.

              O(1)                       O(Log n)                         O(n)
 ▲                              ▲                              ▲
 │                              │                              │
 │                              │                              │                      .
 │                              │                              │                    .
 │                              │                              │                  .
 │                              │                              │                .
 │                              │                              │              .
t│                             t│                      ...    t│            .
 │                              │              .........       │          .
 │                              │         ......               │        .
 │                              │     .....                    │      .
 │                              │   ...                        │    .
 │.........................     │ ..                           │  .
 │                              │.                             │.
 └────────────────────────►     └────────────────────────►     └────────────────────────►
              n                              n                              n

           O(n*Log n)                    O(Log 2^n)                       O(2^n)
 ▲                     .        ▲            .                 ▲        .
 │                    ..        │            .                 │        .
 │                    .         │            .                 │        .
 │                    .         │            .                 │        .
 │                    .         │            .                 │        .
 │                   .          │            .                 │       .
 │                  ..          │           .                  │       .
t│                 ..          t│          .                  t│      .
 │               ..             │         .                    │     .
 │              ..              │        .                     │    .
 │           ....               │       .                      │    .
 │       . . .                  │     ..                       │   .
 │   .....                      │  ...                         │  .
 │...                           │...                           │..
 └────────────────────────►     └────────────────────────►     └────────────────────────►
              n                              n                              n

              O(n!)
 ▲    .
 │    .
 │    .
 │    .
 │    .
 │    .
 │    .
t│    .
 │    .
 │   .
 │  .
 │ .
 │.
 └────────────────────────►
              n

To understand the big O notation, let us focus on time complexity and specifically examine the O(n) diagram. This diagram depicts a decline in algorithm performance as the input size increases. In contrast, the O(1) diagram represents an algorithm that consistently performs in constant time, with input size having no impact on its efficiency. Consequently, the latter algorithm generally outperforms the former.

However, it is essential to note that this is not always the case. A O(1) algorithm with a single time-consuming operation might be slower than a O(n) algorithm with multiple operations if the single operation in the first algorithm requires more time to complete than the collective operations in the second algorithm.

The Big O notation of an algorithm can be simplified using the following two rules:

  1. Remove the constants. O(n) + 2*O(n*Log n) + 3*O(K) + 5 is simplified to O(n) + O(n*Log n) + O(K).
  2. Remove non-dominant or slower terms. O(n) + O(n*Log n) + O(K) is simplified to O(n*Log n) because O(n*Log n) is the dominant term.

Different Complexities

Data Structure / Algorithm Time Complexity (Average / Worst Case) Space Complexity
Data Structures
---------------------------------- ---------------------------------------- --------------------
Array Access: O(1), Search: O(n), Insert: O(n) O(n)
Linked List (Singly/Doubly) Access: O(n), Search: O(n), Insert: O(1) O(n)
Stack Access: O(n), Search: O(n), Insert: O(1) O(n)
Queue (Array-based) Access: O(1), Search: O(n), Insert: O(1) O(n)
Queue (Linked List-based) Access: O(1), Search: O(n), Insert: O(1) O(n)
Hash Table Insert/Delete/Search: O(1) (amortized) O(n)
Binary Search Tree (BST) Average: O(log n), Worst: O(n) O(n)
AVL Tree (Balanced BST) Insert/Delete/Search: O(log n) O(n)
Heap (Binary, Max/Min) Insert/Delete: O(log n), Max/Min: O(1) O(n)
Trie Insert/Search/Delete: O(L) (L is length) O(ALPHABET_SIZE * L)
Graph (Adjacency Matrix) Access: O(1), Search: O(V^2), Space: O(V^2) O(V^2)
Graph (Adjacency List) Access: O(V + E), Search: O(V + E) O(V + E)
---------------------------------- ---------------------------------------- --------------------
Algorithms
---------------------------------- ---------------------------------------- --------------------
Linear Search O(n) O(1)
Binary Search O(log n) O(1)
Merge Sort O(n log n) O(n)
Quick Sort Average: O(n log n), Worst: O(n^2) O(log n)
Depth-First Search (DFS) O(V + E) O(V)
Breadth-First Search (BFS) O(V + E) O(V)
Dijkstra's Algorithm O(V^2) (with matrix), O((V + E) log V) (with min heap) O(V)
Floyd-Warshall Algorithm O(V^3) (with matrix) O(V^2)
Binary Tree Traversal (in-order, pre-order, post-order) O(n) O(h) (h is the height)
Traveling Salesman Problem (TSP) O(n!) (Brute Force) O(n)
---------------------------------- ---------------------------------------- --------------------

Running Tests

You can easily run the tests for this project using Jest, a JavaScript testing framework.

Prerequisites

Before running the tests, make sure you have installed the required packages. To install the required packages, run below command:

npm install

Running All Tests

To run all the tests in the project, execute the following command in your terminal:

npm test

Questions

The below table includes all the questions, which I have solved.

Arrays

S.No Questions
1 Two sum problem
2 Sort array of 0s, 1s and 2s
3 Find min and max element in array
4 Move negative element to the end of the array
5 Merge sorted array (with and without extra space)
6 Reverse String)
7 Spiral Matrix)
8 Reverse Integer)
9 Print all divisors)
10 Find length of the longest substring without repeating characters)
11 Trapping Rain Water (google interview - hard)
12 Alternate Swap
13 Find array intersection

Linked List

S.No Questions
1 Implement Stack Using Linked List
2 Find kth node from end of a Linked list
3 Linked List has loop or not
4 Insert in a sorted linked list
5 Reverse linked list
6 Find mid point of LL
7 Remove duplicates from sorted LL

Strings

S.No Questions
1 Check number is armstrong number)
2 Determine if the string is palindrome
3 Count character/letter that is most commonly used in string
4 The classic fizz buzz problem
5 Chunk the array
6 Check provided two strings are anagram of each others
7 Find linked list midpoint

Binary Search

S.No Questions
1 First and last occurrence of element)
2 Peak index in a mountain array)
3 Pivot in rotated sorted array)
4 Search in rotated sorted array)

Patterns

S.No Pattern Level Questions
1 Two Pointers Easy Merge Two Sorted Lists
2 Two Pointers Easy two Sum
3 Two Pointers Easy Squares of a Sorted Array
3 Two Pointers Easy Squares of a Sorted Array
4 Two Pointers Easy Backspace String Compare
5 Two Pointers Easy Moves Zeroes
6 Two Pointers Easy Is Subsequence
7 Two Pointers Medium Find the duplicate numbers

Other Resources

  1. Alogrithms - Part 1 By Coursera
  2. Alogrithms - Part 1 By Coursera
  3. Master the Coding Interview: Data Structures + Algorithms
  4. Strivers A2Z DSA Course/Sheet
  5. Study Guide by Subjects
  6. 14 Patterns to Ace Any Coding Interview Question
  7. Comprehensive Data Structure and Algorithm Study Guide
  8. Object Oriented Programming
  9. Visualizing DSA through Animation

FAQ

1. What is this repository for?

This repository is dedicated to Data Structures and Algorithms (DSA) problems and their solutions. It provides a collection of well-documented DSA problems with clear explanations and code samples.

2. What programming language is used in this repository?

The solutions are primarily provided in TypeScript, a statically typed superset of JavaScript. However, you can often adapt the concepts and logic to other programming languages.

3. How do I navigate through the DSA problems?

You can use the table of contents (TOC) at the beginning of this README to jump to specific sections. Each problem is listed with a link for easy access.

4. Can I contribute to this repository?

Absolutely! We welcome contributions from the community. If you have new problems to add, improvements to existing solutions, or suggestions for better explanations, please feel free to open a pull request.

5. Are there any recommended resources for learning DSA?

Yes, we have listed some additional resources in the "Other Resources" section. These include online courses and sheets that can help you deepen your understanding of DSA.

6. How can I get started with solving DSA problems?

We recommend starting with the "Questions" section, where you'll find a variety of problems to solve. Begin with the ones that match your skill level and gradually work your way up.

7. Are there any prerequisites for using this repository?

Basic knowledge of programming and data structures is helpful, but even if you're new to DSA, you can use this repository as a learning resource. The problems are organized from easy to more challenging.

8. How can I report issues or seek help?

If you encounter issues with the code or have questions about specific problems, feel free to open an issue in this repository. The community and maintainers will be happy to assist you.

9. Is there a license for this repository?

Yes, this repository is open-source and available under the License mentioned in this README. Please review the license terms before using or contributing to the repository.

License

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

About

This is an open source repo for learning and rehearsing data structures and algorithms in Javascript

Topics

Resources

License

Stars

Watchers

Forks