├── .github └── workflows │ ├── close_stale_issues_and_prs.yml │ ├── generate_coding_json.yml │ ├── generate_system_design_json.yml │ ├── test_coding_json.yml │ └── test_system_design_json.yml ├── LICENSE ├── README.md ├── Rakefile ├── _coding_template ├── README_en.md ├── SOLUTION_en.md ├── meta.yml ├── starter.c ├── starter.cpp ├── starter.go ├── starter.java ├── starter.js ├── starter.py └── starter.rb ├── _system_design_template ├── README_en.md ├── SOLUTION_en.md └── meta.yml ├── coding.json ├── coding ├── alien_dictionary │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── buy_sell_stock │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── climbing_stairs │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── coin_change │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── coin_change_2 │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── contains_duplicate │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── copy_graph │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.js │ └── starter.py ├── counting_bits │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── course_schedule │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── find_median_from_data_stream │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── find_minimum_in_rotated_sorted_array │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── insert_interval │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── knapsack │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.cpp │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── longest_common_subsequence │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── longest_increasing_subsequence │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── longest_substring_without_repeating_characters │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── lru_cache │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── maximum_product_subarray │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── maximum_subarray │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── meeting_rooms │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── meeting_rooms_2 │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── merge_intervals │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── merge_k_sorted_lists │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── merge_two_sorted_linked_lists │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── missing_number │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── number_of_1_bits │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── number_of_islands │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── product_of_array_except_self │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── reverse_bits │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── reverse_linked_list │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── rotate_matrix │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── search_in_rotated_sorted_array │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── spiral_matrix │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── three_sum │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── top_k_frequent_elements │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── two_sum │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── unique_paths │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── valid_anagram │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb └── word_search │ ├── README_en.md │ ├── SOLUTION_en.md │ ├── meta.yml │ ├── starter.c │ ├── starter.cpp │ ├── starter.go │ ├── starter.java │ ├── starter.js │ ├── starter.py │ └── starter.rb ├── system_design.json └── system_design ├── news_feed ├── README_en.md ├── SOLUTION_en.md └── meta.yml ├── online_chat ├── README_en.md ├── SOLUTION_en.md └── meta.yml ├── parking_lot ├── README_en.md ├── SOLUTION_en.md └── meta.yml ├── photo_sharing ├── README_en.md ├── SOLUTION_en.md └── meta.yml ├── ride-share ├── README_en.md ├── SOLUTION_en.md └── meta.yml ├── spending_tracker ├── README_en.md ├── SOLUTION_en.md └── meta.yml ├── url_shortening ├── README_en.md ├── SOLUTION_en.md └── meta.yml └── web_crawler ├── README_en.md ├── SOLUTION_en.md └── meta.yml /.github/workflows/close_stale_issues_and_prs.yml: -------------------------------------------------------------------------------- 1 | name: 'Close stale issues and PRs' 2 | on: 3 | schedule: 4 | - cron: '0 15 * * *' 5 | 6 | jobs: 7 | stale: 8 | runs-on: ubuntu-latest 9 | steps: 10 | - uses: actions/stale@v4 11 | with: 12 | stale-pr-message: 'The PR has been stale (i.e. open without new comments nor commits) for the last 30 days. A stale label is added' 13 | close-pr-message: 'The PR has been closed after 35 days of no activity' 14 | stale-issue-message: 'The issue has been stale (i.e. open without new comments nor commits) for the last 30 days. A stale label is added' 15 | close-issue-message: 'The issue has been closed after 35 days of no activity' 16 | days-before-pr-stale: 30 17 | days-before-pr-close: 5 18 | days-before-issue-stale: 30 19 | days-before-issue-close: 5 20 | -------------------------------------------------------------------------------- /.github/workflows/generate_coding_json.yml: -------------------------------------------------------------------------------- 1 | name: Generate Coding JSON 2 | 3 | on: 4 | push: 5 | paths: 6 | - 'coding/**' 7 | - 'coding/**/**' 8 | branches: 9 | - main 10 | 11 | jobs: 12 | build: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: ruby/setup-ruby@v1 17 | with: 18 | ruby-version: 3.0 19 | bundler-cache: true 20 | - name: rake generate_coding_json 21 | id: generate-coding-json 22 | run: | 23 | rake generate_coding_json 24 | - name: Commit new coding.json 25 | env: 26 | JSON_VERSION: ${{steps.generate-coding-json.outputs.CODING_JSON_VERSION}} 27 | run: | 28 | git config --global user.name 'HackerPen Bot' 29 | git config --global user.email 'hackerpen@users.noreply.github.com' 30 | git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY 31 | git checkout "${GITHUB_REF:11}" 32 | git add . 33 | git commit -m "Update coding.json version to ${JSON_VERSION}" 34 | git push 35 | -------------------------------------------------------------------------------- /.github/workflows/generate_system_design_json.yml: -------------------------------------------------------------------------------- 1 | name: Generate System Design JSON 2 | 3 | on: 4 | push: 5 | paths: 6 | - 'system_design/**' 7 | - 'system_design/**/**' 8 | branches: 9 | - main 10 | 11 | jobs: 12 | build: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: ruby/setup-ruby@v1 17 | with: 18 | ruby-version: 3.0 19 | bundler-cache: true 20 | - name: rake generate_system_design_json 21 | id: generate-system-design-json 22 | run: | 23 | rake generate_system_design_json 24 | - name: Commit new system_design.json 25 | env: 26 | JSON_VERSION: ${{steps.generate-system-design-json.outputs.SYSTEM_DESIGN_JSON_VERSION}} 27 | run: | 28 | git config --global user.name 'HackerPen Bot' 29 | git config --global user.email 'hackerpen@users.noreply.github.com' 30 | git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY 31 | git checkout "${GITHUB_REF:11}" 32 | git add . 33 | git commit -m "Update system_design.json version to ${JSON_VERSION}" 34 | git push 35 | -------------------------------------------------------------------------------- /.github/workflows/test_coding_json.yml: -------------------------------------------------------------------------------- 1 | name: Test Coding JSON 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - main 7 | workflow_dispatch: 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - uses: ruby/setup-ruby@v1 15 | with: 16 | ruby-version: 3.0 17 | bundler-cache: true 18 | - name: DRY_RUN rake generate_coding_json 19 | id: generate-coding-json 20 | run: | 21 | DRY_RUN=true rake generate_coding_json 22 | - name: Echo new version 23 | env: 24 | JSON_VERSION: ${{steps.generate-coding-json.outputs.CODING_JSON_VERSION}} 25 | run: | 26 | echo "New Coding JSON version: $JSON_VERSION" 27 | -------------------------------------------------------------------------------- /.github/workflows/test_system_design_json.yml: -------------------------------------------------------------------------------- 1 | name: Test System Design JSON 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - main 7 | workflow_dispatch: 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - uses: ruby/setup-ruby@v1 15 | with: 16 | ruby-version: 3.0 17 | bundler-cache: true 18 | - name: DRY_RUN rake generate_system_design_json 19 | id: generate-system-design-json 20 | run: | 21 | DRY_RUN=true rake generate_system_design_json 22 | - name: Echo new version 23 | env: 24 | JSON_VERSION: ${{steps.generate-system-design-json.outputs.SYSTEM_DESIGN_JSON_VERSION}} 25 | run: | 26 | echo "New system design JSON version: $JSON_VERSION" 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 HackerPen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tech interview question bank 2 | 3 | Open source tech interview questions, which is integrated with the mock interview platform [HackerPen](https://hackerpen.io). 4 | 5 | ## Purpose 6 | 7 | The primary goal for this project is to establish a transparent, comprehensive question bank for mock interviews on HackerPen. When the bank is transparent, people can study questions in their own time, and then practice it real time with others. When the bank is comprehensive, it should include questions not only used by big tech companies (Amazon, Facebook, Google, etc.), but also include questions suitable for startups and other smaller companies. 8 | 9 | The question bank includes the following types of questions: 10 | 11 | - coding 12 | - system design 13 | 14 | ## Contribute 15 | 16 | **HOW-TO: add a coding question** 17 | 18 | - fork this repo, and create a new branch 19 | - copy a new `_coding_template/` to `coding/` directory 20 | - modify the copied `_coding_template` 21 | - commit the change, push the branch, open a pull request 22 | 23 | **HOW-TO: add a system design question** 24 | 25 | - fork this repo, and create a new branch 26 | - copy a new `_system_design_template/` to `system_design/` directory 27 | - modify the copied `_system_design_template` 28 | - commit the change, push the branch, open a pull request 29 | 30 | ## Acknowledgements 31 | 32 | The content in this repository is sourced from community contributors and following sources: 33 | 34 | - [System Design Primer](https://github.com/donnemartin/system-design-primer) 35 | - [Interactive Coding Challenges](https://github.com/donnemartin/interactive-coding-challenges) 36 | - [LeetCode](https://leetcode.com/problemset/all/) 37 | -------------------------------------------------------------------------------- /_coding_template/README_en.md: -------------------------------------------------------------------------------- 1 | ## Title for the question 2 | 3 | What is the problem candidate is trying to solve? What are the essential ideas? 4 | 5 | ### Examples 6 | 7 | Example 1 8 | ``` 9 | Input: 10 | ["this_is_an_input"] 11 | 12 | Output: 13 | "this is the output" 14 | ``` 15 | 16 | Example 2 17 | ``` 18 | Input: 19 | ["this_is_an_input"] 20 | 21 | Output: 22 | "this is the output" 23 | ``` 24 | 25 | **constraints** 26 | 27 | - constraint 1 28 | - constraint 2 29 | -------------------------------------------------------------------------------- /_coding_template/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Title for the solution 2 | 3 | 4 | ### Approach 1: 5 | 6 | **algorithm** 7 | 8 | describe how to approach the problem. 9 | 10 | **implementation** 11 | 12 | ```python 13 | def solution(self): 14 | pass 15 | ``` 16 | 17 | **complexity** 18 | 19 | Time complexity: O(N) 20 | Space complexity: O(N) 21 | -------------------------------------------------------------------------------- /_coding_template/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: some_unique_question_identifier 3 | name: "Some Unique Identifier" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: easy 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | - finance 21 | - education 22 | - ecommerce 23 | - social_media 24 | - dev_tools 25 | -------------------------------------------------------------------------------- /_coding_template/starter.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackerPen/tech-interview-questions/e6958bd582a586a7e28bfe9db7475565db0c2433/_coding_template/starter.c -------------------------------------------------------------------------------- /_coding_template/starter.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackerPen/tech-interview-questions/e6958bd582a586a7e28bfe9db7475565db0c2433/_coding_template/starter.cpp -------------------------------------------------------------------------------- /_coding_template/starter.go: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackerPen/tech-interview-questions/e6958bd582a586a7e28bfe9db7475565db0c2433/_coding_template/starter.go -------------------------------------------------------------------------------- /_coding_template/starter.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackerPen/tech-interview-questions/e6958bd582a586a7e28bfe9db7475565db0c2433/_coding_template/starter.java -------------------------------------------------------------------------------- /_coding_template/starter.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackerPen/tech-interview-questions/e6958bd582a586a7e28bfe9db7475565db0c2433/_coding_template/starter.js -------------------------------------------------------------------------------- /_coding_template/starter.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackerPen/tech-interview-questions/e6958bd582a586a7e28bfe9db7475565db0c2433/_coding_template/starter.py -------------------------------------------------------------------------------- /_coding_template/starter.rb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackerPen/tech-interview-questions/e6958bd582a586a7e28bfe9db7475565db0c2433/_coding_template/starter.rb -------------------------------------------------------------------------------- /_system_design_template/README_en.md: -------------------------------------------------------------------------------- 1 | # Title for the question 2 | 3 | What is the problem candidate is trying to solve? What are the essential ideas? 4 | 5 | ## Requirements: 6 | 7 | In actual interview, one should always clarify requirements at the beginning of the interview. 8 | In mock interview, the requirements are specified to help the users move forward 9 | 10 | **Functional Requirements**: 11 | 12 | 1. Feature 1 that a customer can do 13 | 2. Feature 2 that a customer can do 14 | 15 | **Non-Functional Requirements**: 16 | 17 | 1. (option) The system should be highly available. *Add what this means for this question*. 18 | 2. (option) The system should be real-time. *Add what this means for this question*. 19 | 3. (option) The system should be resilient against malicious behavior. *Add what this means for this question* 20 | -------------------------------------------------------------------------------- /_system_design_template/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | # Solution for the question 2 | 3 | ## requirements 4 | 5 | Make sure to discuss **functional requirements** and **non-functional requirements**. 6 | 7 | ## Capacity Estimate and Constraints 8 | 9 | **Traffic estimates**: 10 | 11 | Make some assumptions, then carry out maths 12 | 13 | **Storage estimates**: 14 | 15 | Make some assumptions, then carry out maths 16 | 17 | **Bandwidth estimates**: 18 | 19 | Make some assumptions, then carry out maths 20 | 21 | **Memory estimates**: 22 | 23 | Make some assumptions, then carry out maths 24 | 25 | ## System APIs 26 | 27 | Design RESTful APIs or GraphQL to build the functionality of the service. 28 | 29 | 30 | ## Database Design 31 | 32 | Define the DB Schema, and decide what kind of database to use (e.g. SQL vs NoSQL) 33 | 34 | 35 | ## Scaling 36 | 37 | Please cover scaling items, include but not limited to: 38 | 39 | - how to scale the system handle 1 million, 10 million, 100 million users? (Site Reliability) 40 | - how to ensure the system is working properly as expected, how to get notified when there is a problem? (Observability) 41 | -------------------------------------------------------------------------------- /_system_design_template/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: some_unique_question_identifier 3 | name: "Some Unique Identifier" 4 | category: system_design 5 | difficulty: medium 6 | description: 7 | en: README_en.md 8 | solution: 9 | en: SOLUTION_en.md 10 | industries: 11 | - finance 12 | - education 13 | - ecommerce 14 | - social_media 15 | - dev_tools 16 | -------------------------------------------------------------------------------- /coding/alien_dictionary/README_en.md: -------------------------------------------------------------------------------- 1 | ## Alien Dictionary 2 | 3 | There is a new alien language that uses the English alphabet. However, the order among the letters is unknown to you. 4 | 5 | You are given a list of strings words from the alien language's dictionary, where the strings in words are **sorted lexicographically** by the rules of this new language. 6 | 7 | Return a string of the unique letters in the new alien language sorted in **lexicographically increasing order** by the new language's rules. If there is no solution, return "". If there are multiple solutions, return any of them. 8 | 9 | A string s is **lexicographically smaller** than a string t if at the first letter where they differ, the letter in s comes before the letter in t in the alien language. If the first min(s.length, t.length) letters are the same, then s is smaller if and only if s.length < t.length. 10 | 11 | ### Examples 12 | 13 | Example 1 14 | ``` 15 | Input: words = ["wrt","wrf","er","ett","rftt"] 16 | Output: "wertf" 17 | ``` 18 | 19 | Example 2 20 | ``` 21 | Input: words = ["z","x"] 22 | Output: "zx" 23 | ``` 24 | 25 | Example 3 26 | ``` 27 | Input: words = ["z","x","z"] 28 | Output: "" 29 | Explanation: The order is invalid, so return "". 30 | ``` 31 | 32 | **constraints** 33 | 34 | - 1 <= words.length <= 100 35 | - 1 <= words[i].length <= 100 36 | - words[i] consists of only lowercase English letters. 37 | -------------------------------------------------------------------------------- /coding/alien_dictionary/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_alien_dictionary 3 | name: "Alien Dictionary" 4 | category: coding 5 | subcategory: "graph" 6 | difficulty: hard 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | - education 21 | -------------------------------------------------------------------------------- /coding/alien_dictionary/starter.c: -------------------------------------------------------------------------------- 1 | char * alienOrder(char ** words, int wordsSize){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/alien_dictionary/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string alienOrder(vector& words) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/alien_dictionary/starter.go: -------------------------------------------------------------------------------- 1 | func alienOrder(words []string) string { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/alien_dictionary/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String alienOrder(String[] words) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/alien_dictionary/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string[]} words 3 | * @return {string} 4 | */ 5 | var alienOrder = function(words) { 6 | 7 | }; 8 | -------------------------------------------------------------------------------- /coding/alien_dictionary/starter.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def alienOrder(self, words): 3 | """ 4 | :type words: List[str] 5 | :rtype: str 6 | """ 7 | 8 | -------------------------------------------------------------------------------- /coding/alien_dictionary/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {String[]} words 2 | # @return {String} 3 | def alien_order(words) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/buy_sell_stock/README_en.md: -------------------------------------------------------------------------------- 1 | ## Best Time to Buy and Sell Stock 2 | 3 | You are given an array prices where prices[i] is the price of a given stock on the ith day. 4 | 5 | You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. 6 | 7 | Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. 8 | 9 | 10 | 11 | **Example 1**: 12 | 13 | ``` 14 | Input: prices = [7,1,5,3,6,4] 15 | Output: 5 16 | Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. 17 | Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. 18 | ``` 19 | 20 | **Example 2**: 21 | 22 | ``` 23 | Input: prices = [7,6,4,3,1] 24 | Output: 0 25 | Explanation: In this case, no transactions are done and the max profit = 0. 26 | ``` 27 | 28 | **Constraints**: 29 | 30 | - 1 <= prices.length <= 105 31 | - 0 <= prices[i] <= 104 32 | -------------------------------------------------------------------------------- /coding/buy_sell_stock/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for buy and sell stocks 2 | 3 | We need to find out the maximum difference (which will be the maximum profit) between two numbers in the given array. Also, the second number (selling price) must be larger than the first one (buying price). 4 | 5 | In formal terms, we need to find max(prices[j]−prices[i]), for every i and j such that j > i. 6 | 7 | ### Approach 1: Brute Force 8 | 9 | **implementation** 10 | 11 | ```java 12 | public class Solution { 13 | public int maxProfit(int prices[]) { 14 | int maxprofit = 0; 15 | for (int i = 0; i < prices.length - 1; i++) { 16 | for (int j = i + 1; j < prices.length; j++) { 17 | int profit = prices[j] - prices[i]; 18 | if (profit > maxprofit) 19 | maxprofit = profit; 20 | } 21 | } 22 | return maxprofit; 23 | } 24 | } 25 | ``` 26 | 27 | ```python 28 | class Solution: 29 | def maxProfit(self, prices: List[int]) -> int: 30 | max_profit = 0 31 | for i in range(len(prices) - 1): 32 | for j in range(i + 1, len(prices)): 33 | profit = prices[j] - prices[i] 34 | if profit > max_profit: 35 | max_profit = profit 36 | 37 | return max_profit 38 | ``` 39 | 40 | **Complexity Analysis** 41 | 42 | - Time complexity: O(n^2) 43 | - Space complexity: O(1). Only two variables - maxprofit and profit are used. 44 | 45 | ### Solution 2: track buy price and max profit 46 | **algorithm** 47 | Initialize two variables `buy_price=prices[0]` and `max_profit=0` 48 | Iterate through prices, and compare the price with buy price: 49 | - if it's bigger than current buy price, then calculate `profit = price - buyer_price`, compare it with existing `max_profit`. 50 | - if it's smaller than current buy price, then set it as the new buy price. 51 | 52 | **implementation** 53 | 54 | ```ruby 55 | def max_profit(prices) 56 | buy_price = prices[0] 57 | max_profit = 0 58 | 59 | prices.each do |price| 60 | if price > buy_price 61 | max_profit = [(price - buy_price), max_profit].max 62 | elsif price < buy_price 63 | buy_price = price 64 | end 65 | end 66 | 67 | max_profit 68 | end 69 | ``` 70 | 71 | **Complexity Analysis** 72 | 73 | - Time complexity: O(n), where n is the size of prices. It iterates through the prices once. 74 | - Space complexity: O(1). Only two variables - `buy_price` and `max_profit` are used. 75 | 76 | -------------------------------------------------------------------------------- /coding/buy_sell_stock/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_buy_sell_stock 3 | name: "Buy & Sell Stock" 4 | category: coding 5 | subcategory: "array" 6 | description: 7 | en: README_en.md 8 | solution: 9 | en: SOLUTION_en.md 10 | starter_code: 11 | c: "starter.c" 12 | cpp: "starter.cpp" 13 | go: "starter.go" 14 | javascript: "starter.js" 15 | python: "starter.py" 16 | ruby: "starter.rb" 17 | industries: 18 | - finance 19 | difficulty: easy 20 | -------------------------------------------------------------------------------- /coding/buy_sell_stock/starter.c: -------------------------------------------------------------------------------- 1 | int maxProfit(int* prices, int pricesSize){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/buy_sell_stock/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/buy_sell_stock/starter.go: -------------------------------------------------------------------------------- 1 | func maxProfit(prices []int) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/buy_sell_stock/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} prices 3 | * @return {number} 4 | */ 5 | var maxProfit = function(prices) { 6 | 7 | }; 8 | -------------------------------------------------------------------------------- /coding/buy_sell_stock/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - prices: List[int] 4 | # @return int 5 | def maxProfit(self, prices): 6 | pass 7 | -------------------------------------------------------------------------------- /coding/buy_sell_stock/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} prices 2 | # @return {Integer} 3 | def max_profit(prices) 4 | end 5 | 6 | puts(max_profit([7,1,5,3,6,4])) # return 5 7 | puts(max_profit([7,6,4,3,1])) # return 0 8 | -------------------------------------------------------------------------------- /coding/climbing_stairs/README_en.md: -------------------------------------------------------------------------------- 1 | ## Climbing Stairs 2 | 3 | You are climbing a staircase. It takes n steps to reach the top. 4 | 5 | Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 6 | 7 | ### Examples 8 | 9 | Example 1 10 | ``` 11 | Input: n = 2 12 | Output: 2 13 | Explanation: There are two ways to climb to the top. 14 | 1. 1 step + 1 step 15 | 2. 2 steps 16 | ``` 17 | 18 | Example 2 19 | ``` 20 | Input: n = 3 21 | Output: 3 22 | Explanation: There are three ways to climb to the top. 23 | 1. 1 step + 1 step + 1 step 24 | 2. 1 step + 2 steps 25 | 3. 2 steps + 1 step 26 | ``` 27 | 28 | **constraints** 29 | 30 | - 1 <= n <= 45 31 | -------------------------------------------------------------------------------- /coding/climbing_stairs/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_climbing_stairs 3 | name: "Climbing Stairs" 4 | category: coding 5 | subcategory: "dynamic_programming" 6 | difficulty: easy 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | - finance 21 | - education 22 | - ecommerce 23 | - social_media 24 | - dev_tools 25 | -------------------------------------------------------------------------------- /coding/climbing_stairs/starter.c: -------------------------------------------------------------------------------- 1 | int climbStairs(int n){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/climbing_stairs/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int climbStairs(int n) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/climbing_stairs/starter.go: -------------------------------------------------------------------------------- 1 | func climbStairs(n int) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/climbing_stairs/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int climbStairs(int n) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/climbing_stairs/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {number} 4 | */ 5 | var climbStairs = function(n) { 6 | 7 | }; 8 | -------------------------------------------------------------------------------- /coding/climbing_stairs/starter.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def climbStairs(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | 8 | -------------------------------------------------------------------------------- /coding/climbing_stairs/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer} n 2 | # @return {Integer} 3 | def climb_stairs(n) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/coin_change/README_en.md: -------------------------------------------------------------------------------- 1 | ## Coin change 2 | 3 | You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. 4 | 5 | Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1. 6 | 7 | You may assume that you have an infinite number of each kind of coin. 8 | 9 | 10 | **Example 1:** 11 | 12 | ``` 13 | Input: coins = [1,2,5], amount = 11 14 | Output: 3 15 | Explanation: 11 = 5 + 5 + 1 16 | ``` 17 | 18 | **Example 2:** 19 | 20 | ``` 21 | Input: coins = [2], amount = 3 22 | Output: -1 23 | ``` 24 | 25 | **Example 3:** 26 | 27 | ``` 28 | Input: coins = [1], amount = 0 29 | Output: 0 30 | ``` 31 | 32 | ### Constraints: 33 | 34 | 1 <= coins.length <= 12 35 | 1 <= coins[i] <= 231 - 1 36 | 0 <= amount <= 104 37 | -------------------------------------------------------------------------------- /coding/coin_change/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_coin_change 3 | name: "Coin Change" 4 | category: coding 5 | subcategory: "dynamic_programming" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/coin_change/starter.c: -------------------------------------------------------------------------------- 1 | int coinChange(int* coins, int coinsSize, int amount){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/coin_change/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int coinChange(vector& coins, int amount) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/coin_change/starter.go: -------------------------------------------------------------------------------- 1 | func coinChange(coins []int, amount int) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/coin_change/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int coinChange(int[] coins, int amount) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/coin_change/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} coins 3 | * @param {number} amount 4 | * @return {number} 5 | */ 6 | var coinChange = function(coins, amount) { 7 | 8 | }; 9 | -------------------------------------------------------------------------------- /coding/coin_change/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # coins: List[int] 4 | # amount: int 5 | # @return: int 6 | def coinChange(self, coins, amount): 7 | -------------------------------------------------------------------------------- /coding/coin_change/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} coins 2 | # @param {Integer} amount 3 | # @return {Integer} 4 | def coin_change(coins, amount) 5 | end 6 | 7 | puts(coin_change([1,2,5], 11)) # 3 8 | puts(coin_change([2], 3)) # -1 9 | -------------------------------------------------------------------------------- /coding/coin_change_2/README_en.md: -------------------------------------------------------------------------------- 1 | ## Coin Change 2 2 | 3 | You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. 4 | 5 | Return the number of combinations that make up that amount. If that amount of money cannot be made up by any combination of the coins, return 0. 6 | 7 | You may assume that you have an infinite number of each kind of coin. 8 | 9 | The answer is guaranteed to fit into a signed 32-bit integer. 10 | 11 | 12 | ### Examples 13 | 14 | Example 1 15 | ``` 16 | nput: amount = 5, coins = [1,2,5] 17 | Output: 4 18 | Explanation: there are four ways to make up the amount: 19 | 5=5 20 | 5=2+2+1 21 | 5=2+1+1+1 22 | 5=1+1+1+1+1 23 | ``` 24 | 25 | Example 2 26 | ``` 27 | Input: amount = 3, coins = [2] 28 | Output: 0 29 | Explanation: the amount of 3 cannot be made up just with coins of 2 30 | ``` 31 | 32 | Example 3 33 | ``` 34 | Input: amount = 10, coins = [10] 35 | Output: 1 36 | ``` 37 | 38 | **constraints** 39 | 40 | - 1 <= coins.length <= 300 41 | - 1 <= coins[i] <= 5000 42 | - All the values of coins are unique. 43 | - 0 <= amount <= 5000 44 | 45 | -------------------------------------------------------------------------------- /coding/coin_change_2/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for Coin Change 2 2 | 3 | 4 | ### Approach 1: Dynamic Programming (bottom-up) 5 | 6 | **algorithm** 7 | 8 | To solve `coin_change(amount, coins)` problems, we must solve its subproblems. 9 | For example, to know how to exchange $10 using $1, $2, $5 coins, 10 | We first solve exchanging $1 using the same set of coins, then $2, $3.. 11 | And we store the solution to these sub-problems in an array `dp`. 12 | `dp` is initialized with `0` for each amount as a placeholder solution. 13 | Although one solution is true - `dp[0] = 1` because there is only one way to return changes to `$0` 14 | It is to return no coins. 15 | 16 | A generic solution emerges 17 | `dp[amount] = dp[amount] + dp[amount-coin]` for all `coin < amount` 18 | 19 | **implementation** 20 | 21 | ```ruby 22 | def coin_change(amount, coins) 23 | dp = [0] * (amount + 1) 24 | dp[0] = 1 25 | 26 | coins.each do |coin| 27 | [coin..amount].to_a.each do |x| 28 | dp[x] = dp[x] + dp[x - coin] 29 | end 30 | end 31 | 32 | dp[amount] 33 | end 34 | ``` 35 | 36 | **complexity** 37 | 38 | Time complexity: O(N *S), where `N` is the amount, `S` is the number of coins 39 | Space complexity: O(N), where `N` is the amount 40 | -------------------------------------------------------------------------------- /coding/coin_change_2/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_coin_change_2 3 | name: "Coin Change 2" 4 | category: coding 5 | subcategory: "dynamic_programming" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/coin_change_2/starter.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | int change(int amount, int* coins, int coinsSize){ 4 | 5 | } 6 | -------------------------------------------------------------------------------- /coding/coin_change_2/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int change(int amount, vector& coins) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/coin_change_2/starter.go: -------------------------------------------------------------------------------- 1 | func change(amount int, coins []int) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/coin_change_2/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int change(int amount, int[] coins) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/coin_change_2/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} amount 3 | * @param {number[]} coins 4 | * @return {number} 5 | */ 6 | var change = function(amount, coins) { 7 | 8 | }; 9 | -------------------------------------------------------------------------------- /coding/coin_change_2/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def change(self, amount, coins): 3 | pass 4 | 5 | -------------------------------------------------------------------------------- /coding/coin_change_2/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer} amount 2 | # # @param {Integer[]} coins 3 | # # @return {Integer} 4 | def change(amount, coins) 5 | 6 | end 7 | -------------------------------------------------------------------------------- /coding/contains_duplicate/README_en.md: -------------------------------------------------------------------------------- 1 | ## Contains Duplicate 2 | 3 | Given an integer array, return true if there is any value that appears more than once in the array. Return false if every element is unique 4 | 5 | ### Examples 6 | 7 | Example 1 8 | ``` 9 | Input: nums = [1,2,3,1] 10 | Output: true 11 | ``` 12 | 13 | Example 2 14 | ``` 15 | Input: nums = [1,2,3,4] 16 | Output: false 17 | ``` 18 | 19 | Example 3: 20 | ``` 21 | Input: nums = [1,1,1,3,3,4,3,2,4,2] 22 | Output: true 23 | ``` 24 | -------------------------------------------------------------------------------- /coding/contains_duplicate/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for Contains Duplicate 2 | 3 | 4 | ### Approach 1: brute force 5 | 6 | **algorithm** 7 | 8 | For every element, compare to every other element 9 | 10 | **implementation** 11 | 12 | ```java 13 | public boolean hasDuplicate(int[] nums) { 14 | for (int i = 0; i < nums.length; ++i) { 15 | for (int j = 0; j < i; ++j) { 16 | if (nums[j] == nums[i]) return true; 17 | } 18 | } 19 | return false; 20 | } 21 | ``` 22 | **Complexity Analysis** 23 | 24 | - Time complexity: O(N^2) for N elements 25 | - Space complexity: O(1) 26 | 27 | ### Approach 2: Sort 28 | 29 | **algorithm** 30 | 31 | Sort array ascending. Compare each element to the element to its following element. 32 | 33 | Make sure you're allowed to modify the input. Otherwise, make a copy and sort for this solution. 34 | 35 | **implementation** 36 | 37 | ```java 38 | public boolean hasDuplicate(int[] nums) { 39 | Arrays.sort(nums); 40 | for (int i = 0; i < nums.length - 1; ++i) { 41 | if (nums[i] == nums[i + 1]) return true; 42 | } 43 | return false; 44 | } 45 | ``` 46 | **Complexity Analysis** 47 | 48 | - Time complexity: O(N log N) for sorting N elements, O(N) to search. O(N log N) dominates 49 | - Space complexity: O(log N) for recursive stack space of sort algorithm 50 | 51 | 52 | 53 | ### Approach 3: Hash Set 54 | 55 | **algorithm** 56 | 57 | What data structure has fast search and insert? Hash Set is perfect when dealing with duplicates. 58 | Iterate through the array and check if we've already seen it. Otherwise add to hash set and keep checking. If we've iterated through the whole array without finding a duplicate, we can return false. 59 | 60 | - make sure we use a hash set here instead of a hash table as there is no need for payload. 61 | **implementation** 62 | 63 | ```python 64 | 65 | class Solution: 66 | def hasDuplicate(self, nums: List[int]) -> bool: 67 | hash_set = set() 68 | for num in nums: 69 | if num in hash_set: 70 | return True 71 | hash_set.add(num) 72 | return False 73 | ``` 74 | 75 | ```java 76 | public boolean hasDuplicate(int[] nums) { 77 | Set set = new HashSet<>(nums.length); 78 | for (int x: nums) { 79 | if (set.contains(x)) return true; 80 | set.add(x); 81 | } 82 | return false; 83 | } 84 | ``` 85 | 86 | ```javascript 87 | const hasDuplicate = nums => { 88 | const hashSet = new Set(); 89 | for (const num of nums){ 90 | if (hashSet.has(num)) return true; 91 | hashSet.add(num); 92 | } 93 | return false; 94 | } 95 | ``` 96 | 97 | **Complexity Analysis** 98 | 99 | - Time complexity: O(N) for sorting N elements, O(N) to search. O(N log N) dominates 100 | - Space complexity: O(N) for recursive stack space of sort algorithm 101 | -------------------------------------------------------------------------------- /coding/contains_duplicate/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_contains_duplicate 3 | name: "Contains Duplicate" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: easy 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/contains_duplicate/starter.c: -------------------------------------------------------------------------------- 1 | bool hasDuplicate(int* nums, int numsSize){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/contains_duplicate/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool hasDuplicate(vector& nums) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/contains_duplicate/starter.go: -------------------------------------------------------------------------------- 1 | func hasDuplicate(nums []int) bool { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/contains_duplicate/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean hasDuplicate(int[] nums) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/contains_duplicate/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {boolean} 4 | */ 5 | const hasDuplicate = (nums) => { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /coding/contains_duplicate/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - nums: List[int] 4 | # @return: bool 5 | def hasDuplicate(self, nums): 6 | pass 7 | -------------------------------------------------------------------------------- /coding/contains_duplicate/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @return {Boolean} 3 | def has_duplicate(nums) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/copy_graph/README_en.md: -------------------------------------------------------------------------------- 1 | ## Copy Graph 2 | 3 | Given any node from a undirected connected graph, create and return a **deep copy** of the entire graph (you can return the deep copy of the starting node). 4 | A deep copy of a graph creates new nodes for every single node in the graph (as opposed to only cloning the first, or some graphs). 5 | 6 | Definition of a node: 7 | 8 | ```python 9 | class Node: 10 | def __init__(self, val=0, connections=[]): 11 | self.val = val 12 | self.connections = connections 13 | ``` 14 | 15 | ### Examples 16 | 17 | Example 1 18 | 19 | ```plaintext 20 | Start at node with value 1 21 | With an edge list: [[1,2],[1,3],[1,4]], there are 3 nodes, all connected via node with value 1. 22 | Input: Node(val=1, connections=[2,3,4]) 23 | Output: Node(val=1, connections=[2,3,4]) # This node and it's connections are new nodes, with different memory addresses 24 | ``` 25 | 26 | **constraints** 27 | 28 | - Each node has a unique value 29 | - The graph has one "component" meaning you can visit the entire graph starting from any node 30 | -------------------------------------------------------------------------------- /coding/copy_graph/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Copy Graph 2 | 3 | 4 | ### Approach 1: Recursive Depth First Search (DFS) 5 | 6 | **algorithm** 7 | 8 | The intuition to use recursive DFS stems from a definition of deep copies. Deep copies ensure that the 9 | entire object and all of it's subsidiaries/dependencies (here connections) are duplicated with new memory. 10 | One way to think of this is that you will never use any references to an original object in your deep copy. 11 | This is in contrast to shallow copies, which do not ensure all aspects of a copy are new. 12 | 13 | Using DFS recursively naturally solves this problem in the following implementation 14 | 15 | **implementation** 16 | 17 | ```python 18 | def copy_graph(start_node): 19 | node_mapping = {} 20 | def dfs_copy(node): 21 | if node in node_mapping: # we already have a copy for this node 22 | return node_mapping[node] 23 | copy = Node(val=node.val) 24 | node_mapping[node] = copy 25 | # Add connection's duplicate to copy's connections 26 | for c in node.connections: 27 | copy.connections.append(copy_graph(c)) 28 | return copy 29 | 30 | return copy_graph(start_node) if start_node else None 31 | ``` 32 | 33 | **complexity** 34 | V = Number of nodes/vertices 35 | E = Number of edges 36 | Time complexity: O(V + E) 37 | Space complexity: O(V) - At worse, a mapping of V nodes + a call stack of size V 38 | -------------------------------------------------------------------------------- /coding/copy_graph/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: copy_graph 3 | name: "Copy Graph" 4 | category: coding 5 | subcategory: "graph" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | javascript: "starter.js" 13 | python: "starter.py" 14 | industries: 15 | - software 16 | -------------------------------------------------------------------------------- /coding/copy_graph/starter.js: -------------------------------------------------------------------------------- 1 | function copyGraph(startNode) {} 2 | -------------------------------------------------------------------------------- /coding/copy_graph/starter.py: -------------------------------------------------------------------------------- 1 | def clone_graph(start_node): 2 | pass 3 | -------------------------------------------------------------------------------- /coding/counting_bits/README_en.md: -------------------------------------------------------------------------------- 1 | ## Counting Bits 2 | 3 | Given an integer n, return an array `ans` of length `n + 1` such that for each i `(0 <= i <= n)`, `ans[i]` is the **number of 1's** in the binary representation of i. 4 | 5 | 6 | 7 | ### Examples 8 | 9 | Example 1 10 | ``` 11 | Input: n = 2 12 | Output: [0,1,1] 13 | Explanation: 14 | 0 --> 0 15 | 1 --> 1 16 | 2 --> 10 17 | ``` 18 | 19 | Example 2 20 | ``` 21 | Input: n = 5 22 | Output: [0,1,1,2,1,2] 23 | Explanation: 24 | 0 --> 0 25 | 1 --> 1 26 | 2 --> 10 27 | 3 --> 11 28 | 4 --> 100 29 | 5 --> 101 30 | ``` 31 | 32 | **constraints** 33 | 34 | - 0 <= n <= 10^5 35 | -------------------------------------------------------------------------------- /coding/counting_bits/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_counting_bits 3 | name: "Counting Bits" 4 | category: coding 5 | subcategory: "binary" 6 | difficulty: easy 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/counting_bits/starter.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Note: The returned array must be malloced, assume caller calls free(). 3 | */ 4 | int* countBits(int n, int* returnSize){ 5 | 6 | } 7 | -------------------------------------------------------------------------------- /coding/counting_bits/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector countBits(int n) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/counting_bits/starter.go: -------------------------------------------------------------------------------- 1 | func countBits(n int) []int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/counting_bits/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] countBits(int n) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/counting_bits/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {number[]} 4 | */ 5 | var countBits = function(n) { 6 | 7 | }; 8 | -------------------------------------------------------------------------------- /coding/counting_bits/starter.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countBits(self, n): 3 | """ 4 | :type n: int 5 | :rtype: List[int] 6 | """ 7 | 8 | -------------------------------------------------------------------------------- /coding/counting_bits/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer} n 2 | # @return {Integer[]} 3 | def count_bits(n) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/course_schedule/README_en.md: -------------------------------------------------------------------------------- 1 | ## Course Schedule 2 | 3 | There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai. 4 | 5 | For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1. 6 | Return true if you can finish all courses. Otherwise, return false. 7 | 8 | 9 | 10 | **Example 1**: 11 | 12 | ``` 13 | Input: numCourses = 2, prerequisites = [[1,0]] 14 | Output: true 15 | Explanation: There are a total of 2 courses to take. 16 | To take course 1 you should have finished course 0. So it is possible. 17 | ``` 18 | 19 | **Example 2**: 20 | 21 | ``` 22 | Input: numCourses = 2, prerequisites = [[1,0],[0,1]] 23 | Output: false 24 | Explanation: There are a total of 2 courses to take. 25 | To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible. 26 | ``` 27 | 28 | **Constraints**: 29 | 30 | - 1 <= numCourses <= 105 31 | - 0 <= prerequisites.length <= 5000 32 | - prerequisites[i].length == 2 33 | - 0 <= ai, bi < numCourses 34 | - All the pairs prerequisites[i] are unique. 35 | -------------------------------------------------------------------------------- /coding/course_schedule/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_course_schedule 3 | name: "Course Schedule" 4 | category: coding 5 | subcategory: "graph" 6 | description: 7 | en: README_en.md 8 | solution: 9 | en: SOLUTION_en.md 10 | starter_code: 11 | c: "starter.c" 12 | cpp: "starter.cpp" 13 | go: "starter.go" 14 | javascript: "starter.js" 15 | python: "starter.py" 16 | ruby: "starter.rb" 17 | industries: 18 | - education 19 | difficulty: medium 20 | -------------------------------------------------------------------------------- /coding/course_schedule/starter.c: -------------------------------------------------------------------------------- 1 | bool canFinish(int numCourses, int** prerequisites, int prerequisitesSize, int* prerequisitesColSize){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/course_schedule/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canFinish(int numCourses, vector>& prerequisites) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/course_schedule/starter.go: -------------------------------------------------------------------------------- 1 | func canFinish(numCourses int, prerequisites [][]int) bool { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/course_schedule/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canFinish(int numCourses, int[][] prerequisites) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/course_schedule/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} numCourses 3 | * @param {number[][]} prerequisites 4 | * @return {boolean} 5 | */ 6 | var canFinish = function(numCourses, prerequisites) { 7 | 8 | }; 9 | -------------------------------------------------------------------------------- /coding/course_schedule/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - numCourses: int 4 | # - prerequisites: List[List[int]] 5 | # @return: bool 6 | def canFinish(self, numCourses, prerequisites): 7 | pass 8 | -------------------------------------------------------------------------------- /coding/course_schedule/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer} num_courses 2 | # @param {Integer[][]} prerequisites 3 | # @return {Boolean} 4 | def can_finish(num_courses, prerequisites) 5 | 6 | end 7 | -------------------------------------------------------------------------------- /coding/find_median_from_data_stream/README_en.md: -------------------------------------------------------------------------------- 1 | ## Find Median from Data Stream 2 | 3 | The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the average of the two middle values. 4 | 5 | - For example, for arr = [2,3,4], the median is 3 6 | - For example, for arr = [2,3], the median is (2 + 3) / 2 = 2.5. 7 | 8 | Implement the MedianFinder class: 9 | 10 | **MedianFinder()** 11 | 12 | - initializes the `MedianFinder` object. 13 | 14 | **void addNum(int num)** 15 | 16 | - adds the integer `num` from the data stream to the data structure. 17 | 18 | **double findMedian()** 19 | 20 | - returns the median of all elements so far. Answers within 10^-5 of the actual answer will be accepted. 21 | 22 | ### Examples 23 | 24 | Example 1 25 | 26 | ``` 27 | Input 28 | ["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"] 29 | [[], [1], [2], [], [3], []] 30 | Output 31 | [null, null, null, 1.5, null, 2.0] 32 | 33 | Explanation 34 | MedianFinder medianFinder = new MedianFinder(); 35 | medianFinder.addNum(1); // arr = [1] 36 | medianFinder.addNum(2); // arr = [1, 2] 37 | medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2) 38 | medianFinder.addNum(3); // arr[1, 2, 3] 39 | medianFinder.findMedian(); // return 2.0 40 | ``` 41 | 42 | **Follow up:** 43 | 44 | - If all integer numbers from the stream are in the range [0, 100], how would you optimize your solution? 45 | - If `99%` of all integer numbers from the stream are in the range [0, 100], how would you optimize your solution? 46 | -------------------------------------------------------------------------------- /coding/find_median_from_data_stream/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_find_median_from_data_stream 3 | name: "Find Median From Data Stream" 4 | category: coding 5 | subcategory: "Design" 6 | difficulty: hard 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | industries: 19 | -------------------------------------------------------------------------------- /coding/find_median_from_data_stream/starter.c: -------------------------------------------------------------------------------- 1 | typedef struct { 2 | 3 | } MedianFinder; 4 | 5 | 6 | MedianFinder* medianFinderCreate() { 7 | 8 | } 9 | 10 | void medianFinderAddNum(MedianFinder* obj, int num) { 11 | 12 | } 13 | 14 | double medianFinderFindMedian(MedianFinder* obj) { 15 | 16 | } 17 | 18 | void medianFinderFree(MedianFinder* obj) { 19 | 20 | } 21 | 22 | /** 23 | * Your MedianFinder struct will be instantiated and called as such: 24 | * MedianFinder* obj = medianFinderCreate(); 25 | * medianFinderAddNum(obj, num); 26 | 27 | * double param_2 = medianFinderFindMedian(obj); 28 | 29 | * medianFinderFree(obj); 30 | */ 31 | -------------------------------------------------------------------------------- /coding/find_median_from_data_stream/starter.cpp: -------------------------------------------------------------------------------- 1 | class MedianFinder { 2 | public: 3 | MedianFinder() { 4 | 5 | } 6 | 7 | void addNum(int num) { 8 | 9 | } 10 | 11 | double findMedian() { 12 | 13 | } 14 | }; 15 | 16 | /** 17 | * Your MedianFinder object will be instantiated and called as such: 18 | * MedianFinder* obj = new MedianFinder(); 19 | * obj->addNum(num); 20 | * double param_2 = obj->findMedian(); 21 | */ 22 | -------------------------------------------------------------------------------- /coding/find_median_from_data_stream/starter.go: -------------------------------------------------------------------------------- 1 | type MedianFinder struct { 2 | 3 | } 4 | 5 | 6 | func Constructor() MedianFinder { 7 | 8 | } 9 | 10 | 11 | func (this *MedianFinder) AddNum(num int) { 12 | 13 | } 14 | 15 | 16 | func (this *MedianFinder) FindMedian() float64 { 17 | 18 | } 19 | 20 | 21 | /** 22 | * Your MedianFinder object will be instantiated and called as such: 23 | * obj := Constructor(); 24 | * obj.AddNum(num); 25 | * param_2 := obj.FindMedian(); 26 | */ 27 | -------------------------------------------------------------------------------- /coding/find_median_from_data_stream/starter.java: -------------------------------------------------------------------------------- 1 | class MedianFinder { 2 | 3 | public MedianFinder() { 4 | 5 | } 6 | 7 | public void addNum(int num) { 8 | 9 | } 10 | 11 | public double findMedian() { 12 | 13 | } 14 | } 15 | 16 | /** 17 | * Your MedianFinder object will be instantiated and called as such: 18 | * MedianFinder obj = new MedianFinder(); 19 | * obj.addNum(num); 20 | * double param_2 = obj.findMedian(); 21 | */ 22 | -------------------------------------------------------------------------------- /coding/find_median_from_data_stream/starter.js: -------------------------------------------------------------------------------- 1 | class MedianFinder { 2 | /** 3 | * initialize your data structure here. 4 | */ 5 | constructor() {} 6 | 7 | /** 8 | * @param {number} num 9 | * @return {void} 10 | */ 11 | addNum(num) {} 12 | 13 | /** 14 | * @return {number} 15 | */ 16 | findMedian() {} 17 | } 18 | 19 | /** 20 | * Your MedianFinder object will be instantiated and called as such: 21 | * var obj = new MedianFinder() 22 | * obj.addNum(num) 23 | * var param_2 = obj.findMedian() 24 | */ 25 | -------------------------------------------------------------------------------- /coding/find_median_from_data_stream/starter.py: -------------------------------------------------------------------------------- 1 | class MedianFinder: 2 | def __init__(self): 3 | pass 4 | 5 | # @params: 6 | # - num: int 7 | # @return: None 8 | def addNum(self, num): 9 | pass 10 | 11 | # @return: float 12 | def findMedian(self): 13 | pass 14 | 15 | # Your MedianFinder object will be instantiated and called as such: 16 | # obj = MedianFinder() 17 | # obj.addNum(num) 18 | # param_2 = obj.findMedian() 19 | -------------------------------------------------------------------------------- /coding/find_median_from_data_stream/starter.rb: -------------------------------------------------------------------------------- 1 | class MedianFinder 2 | def initialize() 3 | 4 | end 5 | 6 | 7 | =begin 8 | :type num: Integer 9 | :rtype: Void 10 | =end 11 | def add_num(num) 12 | 13 | end 14 | 15 | 16 | =begin 17 | :rtype: Float 18 | =end 19 | def find_median() 20 | 21 | end 22 | 23 | 24 | end 25 | 26 | # Your MedianFinder object will be instantiated and called as such: 27 | # obj = MedianFinder.new() 28 | # obj.add_num(num) 29 | # param_2 = obj.find_median() 30 | -------------------------------------------------------------------------------- /coding/find_minimum_in_rotated_sorted_array/README_en.md: -------------------------------------------------------------------------------- 1 | ## Find Minimum in Rotated Sorted Array 2 | 3 | An array is sorted in ascending order is rotated some number of times. 4 | 5 | For example, the array nums = [0,1,2,4,5,6,7] might become: 6 | 7 | [4,5,6,7,0,1,2] if it was rotated 4 times. 8 | [0,1,2,4,5,6,7] if it was rotated 7 times. 9 | 10 | Given the sorted rotated array nums of unique elements, return the minimum element of this array. 11 | 12 | You must write an algorithm that runs in O(log n) time. 13 | 14 | ### Examples 15 | 16 | Example 1 17 | ``` 18 | Input: nums = [3,4,5,1,2] 19 | Output: 1 20 | Explanation: The original array was [1,2,3,4,5] rotated 3 times. 21 | ``` 22 | 23 | Example 2 24 | ``` 25 | Input: nums = [4,5,6,7,0,1,2] 26 | Output: 0 27 | Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times. 28 | ``` 29 | 30 | Example 3: 31 | ``` 32 | Input: nums = [11,13,15,17] 33 | Output: 11 34 | Explanation: The original array was [11,13,15,17] and it was rotated 4 times. 35 | `````` 36 | -------------------------------------------------------------------------------- /coding/find_minimum_in_rotated_sorted_array/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for Find Minimum in Rotated Sorted Array 2 | 3 | Brute force is to search the entire array. O(N) for N elements. But this won't take advantage of the nearly sorted state of the array. 4 | 5 | ### Approach 1: Binary Search 6 | 7 | **algorithm** 8 | 9 | Since we are dealing with unique elements in a nearly sorted array, we can use a modified binary search. 10 | 11 | - test if array is rotated by comparing first and last elements. If it is not rotated, we know the minimum element is first element and can return it 12 | 13 | If the array is rotated, we binary search. We are looking for the first unsorted mid element. 14 | - the mid in binary search could be the smallest number, so we have to make sure we include it in the search space OR evaluate it before continuing. Compare the mid to its direct left and right elements to see if either pair is in an unsorted state. If so, the smaller unmber is the minimum. if mid > mid+1, our mid+1 is the minimum. if mid-1 > mid, our mid is the minimum 15 | - otherwise, compare the mid to the left element. If mid is larger than left, we know the left section is sorted. Since we are looking for the minimum, we can cut the search space in half and only search the right side. 16 | - if mid is smaller than the left, we can search the left half. 17 | 18 | - when calculating the mid, we use left + (right-left)//2 to guard against interger overflow 19 | 20 | **implementation** 21 | 22 | ```python 23 | class Solution: 24 | def findMinimum(self, nums: List[int]) -> int: 25 | if len(nums) <= 1 or nums[0] < nums[-1]: 26 | return nums[0] 27 | left, right = 0, len(nums)-1 28 | while left <= right: 29 | mid = left + (right-left)//2 30 | # evaluate mid 31 | if nums[mid] > nums[mid+1]: 32 | return nums[mid+1] 33 | if nums[mid-1] > nums[mid]: 34 | return nums[mid] 35 | # evaluate which side to search 36 | if nums[mid] > nums[left]: 37 | left = mid + 1 38 | else: 39 | right = mid - 1 40 | return -1 41 | ``` 42 | 43 | ```java 44 | class Solution { 45 | public int findMinimum(int[] nums) { 46 | if (nums == null || nums.length == 0) return -1; 47 | if (nums.length == 1) return nums[0]; 48 | if (nums[0] < nums[nums.length-1]) return nums[0]; 49 | 50 | int left = 0; 51 | int right = nums.length-1; 52 | while (left <= right){ 53 | int mid = left + (right-left)/2; 54 | if (nums[mid] > nums[mid+1]) return nums[mid+1]; 55 | if (nums[mid] < nums[mid-1]) return nums[mid]; 56 | 57 | if (nums[mid] > nums[left]){ 58 | left = mid + 1; 59 | } else { 60 | right = mid - 1; 61 | } 62 | } 63 | return -1; 64 | } 65 | } 66 | ``` 67 | 68 | ```javascript 69 | const findMinimum = nums => { 70 | if (nums.length <= 1 || nums[0] < nums[nums.length-1]) return nums[0]; 71 | let left = 0, right = nums.length-1; 72 | while (left <= right){ 73 | let mid = left + Math.floor((right-left)/2); 74 | if (nums[mid] > nums[mid+1]) return nums[mid+1]; 75 | if (nums[mid-1] > nums[mid]) return nums[mid]; 76 | if (nums[mid] > nums[left]) left = mid + 1; 77 | else right = mid - 1; 78 | } 79 | return nums[left]; 80 | } 81 | ``` 82 | 83 | **complexity** 84 | 85 | - Time complexity: O(log N) for binary search 86 | 87 | - Space complexity: O(1) for pointers 88 | -------------------------------------------------------------------------------- /coding/find_minimum_in_rotated_sorted_array/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_find_minimum_in_sorted_array 3 | name: "Find Minimum in Sorted Array" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/find_minimum_in_rotated_sorted_array/starter.c: -------------------------------------------------------------------------------- 1 | int findMinimum(int* nums, int numsSize){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/find_minimum_in_rotated_sorted_array/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMinimum(vector& nums) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/find_minimum_in_rotated_sorted_array/starter.go: -------------------------------------------------------------------------------- 1 | func findMinimum(nums []int) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/find_minimum_in_rotated_sorted_array/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMinimum(int[] nums) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/find_minimum_in_rotated_sorted_array/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | 6 | const findMinimum = nums => { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /coding/find_minimum_in_rotated_sorted_array/starter.py: -------------------------------------------------------------------------------- 1 | class Solution 2 | # @params 3 | # - nums: List[int] 4 | # @return: int 5 | def findMinimum(self, nums): 6 | pass 7 | -------------------------------------------------------------------------------- /coding/find_minimum_in_rotated_sorted_array/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @return {Integer} 3 | def find_minimum(nums) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/insert_interval/README_en.md: -------------------------------------------------------------------------------- 1 | ## Insert Interval 2 | 3 | You are given an array of non-overlapping intervals where each interval is [start,end] and the array is sorted in ascending order by start. You are also given a new interval. 4 | 5 | Insert the new interval into the intervals such that intervals remain sorted in ascending order by start, and intervals does not have any overlapping intervals (merge any overlapping) 6 | 7 | return intervals after insertion 8 | 9 | ### Examples 10 | 11 | Example 1 12 | ``` 13 | Input: intervals = [[1,3],[6,9]], newInterval = [2,5] 14 | Output: [[1,5],[6,9]] 15 | ``` 16 | 17 | Example 2 18 | ``` 19 | Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8] 20 | Output: [[1,2],[3,10],[12,16]] 21 | Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10]. 22 | ``` 23 | -------------------------------------------------------------------------------- /coding/insert_interval/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for Insert Interval 2 | 3 | ### Approach 1: Brute force 4 | 5 | - append new interval 6 | - sort by start 7 | - merge intervals 8 | 9 | **implementation** 10 | 11 | Practice merge intervals and this should be pretty standard implementation. 12 | 13 | ### Approach 2: Greedy 14 | 15 | **algorithm** 16 | 17 | Brute force doesn't take advantage that the input is already sorted and the bottleneck is the sorting algorithm runs again for O(N logN). 18 | 19 | We have three possible conditions: 20 | 21 | - the new interval comes before any interval 22 | - new interval is after all intervals 23 | - new interval overlaps 24 | 25 | - add all intervals starting before or after new interval 26 | - overlapping intervals can be merged into new interval until next interval is no longer overlapping 27 | 28 | **implementation** 29 | 30 | ```python 31 | class Solution: 32 | def insert_interval(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]: 33 | merged = [] 34 | isMerged = False 35 | for start,end in intervals: 36 | if isMerged or end < newInterval[0]: 37 | merged.append([start,end]) 38 | elif start > newInterval[1]: 39 | merged.append(newInterval) 40 | merged.append([start,end]) 41 | isMerged = True 42 | else: 43 | newInterval = [min(newInterval[0], start), max(newInterval[1], end)] 44 | if not isMerged: 45 | merged.append(newInterval) 46 | return merged 47 | ``` 48 | 49 | ```javascript 50 | const insertInterval = (intervals, newInterval) => { 51 | const res = []; 52 | let isMerged = false; 53 | for (const [start, end] of intervals) { 54 | if (isMerged || end < newInterval[0]) res.push([start, end]); 55 | else if (start > newInterval[1]) { 56 | res.push(newInterval); 57 | res.push([start, end]); 58 | isMerged = true; 59 | } else { 60 | newInterval = [ 61 | Math.min(newInterval[0], start), 62 | Math.max(newInterval[1], end) 63 | ]; 64 | } 65 | } 66 | if (!isMerged) res.push(newInterval); 67 | return res; 68 | }; 69 | ``` 70 | 71 | **_complexity_** 72 | 73 | - Time complexity: O(N) one pass through N intervals 74 | 75 | - Space complexity: O(1) other than the output 76 | -------------------------------------------------------------------------------- /coding/insert_interval/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_insert_interval 3 | name: "Insert Interval" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/insert_interval/starter.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Return an array of arrays of size *returnSize. 3 | * The sizes of the arrays are returned as *returnColumnSizes array. 4 | * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free(). 5 | */ 6 | int** insertInterval(int** intervals, int intervalsSize, int* intervalsColSize, int* newInterval, int newIntervalSize, int* returnSize, int** returnColumnSizes){ 7 | 8 | } 9 | -------------------------------------------------------------------------------- /coding/insert_interval/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> insertInterval(vector>& intervals, vector& newInterval) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/insert_interval/starter.go: -------------------------------------------------------------------------------- 1 | func insertInterval(intervals [][]int, newInterval []int) [][]int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/insert_interval/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] insertInterval(int[][] intervals, int[] newInterval) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/insert_interval/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} intervals 3 | * @param {number[]} newInterval 4 | * @return {number[][]} 5 | */ 6 | const insertInterval = (intervals, newInterval) => { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /coding/insert_interval/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - intervals: List[List[int]] 4 | # - newInterview: List[int] 5 | def insert_interval(self, intervals, newInterval): 6 | pass 7 | -------------------------------------------------------------------------------- /coding/insert_interval/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[][]} intervals 2 | # @param {Integer[]} new_interval 3 | # @return {Integer[][]} 4 | def insertInterval(intervals, new_interval) 5 | 6 | end 7 | -------------------------------------------------------------------------------- /coding/knapsack/README_en.md: -------------------------------------------------------------------------------- 1 | ## Knapsack 2 | 3 | Given the weights and profits of `N` items, we are asked to put these items in a knapsack that has a capacity `C`. The goal is to get the maximum profit from the items in the knapsack. Each item can only be selected once, as we don’t have multiple quantities of any item. 4 | 5 | Let’s take Merry’s example, who wants to carry some fruits in the knapsack to get maximum profit. Here are the weights and profits of the fruits: 6 | 7 | ``` 8 | Items: { Apple, Orange, Banana, Melon } 9 | Weights: { 2, 3, 1, 4 } 10 | Profits: { 4, 5, 3, 7 } 11 | Knapsack capacity: 5 12 | ``` 13 | 14 | ### Examples 15 | 16 | Example 1 17 | ``` 18 | input: [1, 6, 10, 16], [1, 2, 3, 5], 7 19 | output: 22 20 | explanation: choose profits 6 and 16 21 | ``` 22 | 23 | Example 2 24 | ``` 25 | input: [1, 6, 10, 16], [1, 2, 3, 5], 6 26 | output: 17 27 | explanation: choose profits 1 and 16 28 | ``` 29 | -------------------------------------------------------------------------------- /coding/knapsack/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_knapsack 3 | name: "Knapsack" 4 | category: coding 5 | subcategory: "dynamic_programming" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | javascript: "starter.js" 13 | python: "starter.py" 14 | cpp: "starter.cpp" 15 | ruby: "starter.rb" 16 | industries: 17 | -------------------------------------------------------------------------------- /coding/knapsack/starter.cpp: -------------------------------------------------------------------------------- 1 | using namespace std; 2 | 3 | #include 4 | #include 5 | 6 | class Knapsack { 7 | public: 8 | int solveKnapsack(const vector &profits, const vector &weights, int capacity) { 9 | // TODO: Write your code here 10 | return -1; 11 | } 12 | }; 13 | 14 | int main(int argc, char *argv[]) { 15 | Knapsack ks; 16 | vector profits = {1, 6, 10, 16}; 17 | vector weights = {1, 2, 3, 5}; 18 | int maxProfit = ks.solveKnapsack(profits, weights, 7); 19 | cout << "Total knapsack profit ---> " << maxProfit << endl; 20 | maxProfit = ks.solveKnapsack(profits, weights, 6); 21 | cout << "Total knapsack profit ---> " << maxProfit << endl; 22 | } 23 | -------------------------------------------------------------------------------- /coding/knapsack/starter.java: -------------------------------------------------------------------------------- 1 | class Knapsack { 2 | 3 | public int solveKnapsack(int[] profits, int[] weights, int capacity) { 4 | // TODO: Write your code here 5 | return -1; 6 | } 7 | 8 | public static void main(String[] args) { 9 | Knapsack ks = new Knapsack(); 10 | int[] profits = {1, 6, 10, 16}; 11 | int[] weights = {1, 2, 3, 5}; 12 | int maxProfit = ks.solveKnapsack(profits, weights, 7); 13 | System.out.println("Total knapsack profit ---> " + maxProfit); 14 | maxProfit = ks.solveKnapsack(profits, weights, 6); 15 | System.out.println("Total knapsack profit ---> " + maxProfit); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /coding/knapsack/starter.js: -------------------------------------------------------------------------------- 1 | let solveKnapsack = function(profits, weights, capacity) { 2 | // TODO: Write your code here 3 | return -1; 4 | }; 5 | 6 | var profits = [1, 6, 10, 16]; 7 | var weights = [1, 2, 3, 5]; 8 | console.log(`Total knapsack profit: ---> ${solveKnapsack(profits, weights, 7)}`); 9 | console.log(`Total knapsack profit: ---> ${solveKnapsack(profits, weights, 6)}`); 10 | -------------------------------------------------------------------------------- /coding/knapsack/starter.py: -------------------------------------------------------------------------------- 1 | def solve_knapsack(profits, weights, capacity): 2 | # TODO: Write your code here 3 | return -1 4 | 5 | def main(): 6 | print(solve_knapsack([1, 6, 10, 16], [1, 2, 3, 5], 7)) 7 | print(solve_knapsack([1, 6, 10, 16], [1, 2, 3, 5], 6)) 8 | 9 | main() 10 | -------------------------------------------------------------------------------- /coding/knapsack/starter.rb: -------------------------------------------------------------------------------- 1 | def solve_knapsack(profits, weights, capacity) 2 | # TODO: Write your code here 3 | return -1 4 | end 5 | -------------------------------------------------------------------------------- /coding/longest_common_subsequence/README_en.md: -------------------------------------------------------------------------------- 1 | ## Longest Common Subsequence 2 | 3 | Given two strings `text1` and `text2`, return the length of their longest *common subsequence*. If there is no common subsequence, return 0. 4 | 5 | A **subsequence** of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters. 6 | - For example, "ace" is a subsequence of "abcde". 7 | 8 | A **common subsequence** of two strings is a subsequence that is common to both strings. 9 | 10 | ### Examples 11 | 12 | Example 1 13 | ``` 14 | Input: text1 = "abcde", text2 = "ace" 15 | Output: 3 16 | Explanation: The longest common subsequence is "ace" and its length is 3. 17 | ``` 18 | 19 | Example 2 20 | ``` 21 | Input: text1 = "abc", text2 = "abc" 22 | Output: 3 23 | Explanation: The longest common subsequence is "abc" and its length is 3. 24 | ``` 25 | 26 | Example 3 27 | ``` 28 | Input: text1 = "abc", text2 = "def" 29 | Output: 0 30 | Explanation: There is no such common subsequence, so the result is 0. 31 | ``` 32 | 33 | **constraints** 34 | 35 | - 1 <= text1.length, text2.length <= 1000 36 | - `text1` and `text2` consist of only lowercase English characters. 37 | -------------------------------------------------------------------------------- /coding/longest_common_subsequence/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_longest_common_subsequence 3 | name: "Longest Common Subsequence" 4 | category: coding 5 | subcategory: "dynamic_programming" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/longest_common_subsequence/starter.c: -------------------------------------------------------------------------------- 1 | int longestCommonSubsequence(char * text1, char * text2){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/longest_common_subsequence/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestCommonSubsequence(string text1, string text2) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/longest_common_subsequence/starter.go: -------------------------------------------------------------------------------- 1 | func longestCommonSubsequence(text1 string, text2 string) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/longest_common_subsequence/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestCommonSubsequence(String text1, String text2) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/longest_common_subsequence/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} text1 3 | * @param {string} text2 4 | * @return {number} 5 | */ 6 | var longestCommonSubsequence = function(text1, text2) { 7 | 8 | }; 9 | -------------------------------------------------------------------------------- /coding/longest_common_subsequence/starter.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def longestCommonSubsequence(self, text1, text2): 3 | """ 4 | :type text1: str 5 | :type text2: str 6 | :rtype: int 7 | """ 8 | 9 | -------------------------------------------------------------------------------- /coding/longest_common_subsequence/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {String} text1 2 | # @param {String} text2 3 | # @return {Integer} 4 | def longest_common_subsequence(text1, text2) 5 | 6 | end 7 | -------------------------------------------------------------------------------- /coding/longest_increasing_subsequence/README_en.md: -------------------------------------------------------------------------------- 1 | ## Longest Increasing Subsequence 2 | 3 | Given an integer array `nums`, return the length of the longest strictly increasing subsequence. 4 | 5 | A **subsequence** is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7]. 6 | 7 | 8 | ### Examples 9 | 10 | Example 1 11 | ``` 12 | Input: nums = [10,9,2,5,3,7,101,18] 13 | Output: 4 14 | Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. 15 | ``` 16 | 17 | Example 2 18 | ``` 19 | Input: nums = [0,1,0,3,2,3] 20 | Output: 4 21 | ``` 22 | 23 | Example 3 24 | ``` 25 | Input: nums = [7,7,7,7,7,7,7] 26 | Output: 1 27 | ``` 28 | 29 | **constraints** 30 | 31 | - 1 <= nums.length <= 2500 32 | - -10^4 <= nums[i] <= 10^4 33 | -------------------------------------------------------------------------------- /coding/longest_increasing_subsequence/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_longest_increasing_subsequence 3 | name: "Longest Increasing Subsequence" 4 | category: coding 5 | subcategory: "dynamic_programming" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/longest_increasing_subsequence/starter.c: -------------------------------------------------------------------------------- 1 | int lengthOfLIS(int* nums, int numsSize){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/longest_increasing_subsequence/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLIS(vector& nums) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/longest_increasing_subsequence/starter.go: -------------------------------------------------------------------------------- 1 | func lengthOfLIS(nums []int) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/longest_increasing_subsequence/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLIS(int[] nums) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/longest_increasing_subsequence/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var lengthOfLIS = function(nums) { 6 | 7 | }; 8 | -------------------------------------------------------------------------------- /coding/longest_increasing_subsequence/starter.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def lengthOfLIS(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | 8 | -------------------------------------------------------------------------------- /coding/longest_increasing_subsequence/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @return {Integer} 3 | def length_of_lis(nums) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/longest_substring_without_repeating_characters/README_en.md: -------------------------------------------------------------------------------- 1 | ## Longest Substring Without Repeating Characters 2 | 3 | Given a string s, find the length of the **longest substring** without repeating characters. 4 | 5 | ### Examples 6 | 7 | Example 1 8 | ``` 9 | Input: s = "abcabcbb" 10 | Output: 3 11 | Explanation: The answer is "abc", with the length of 3. 12 | ``` 13 | 14 | Example 2 15 | ``` 16 | Input: s = "bbbbb" 17 | Output: 1 18 | Explanation: The answer is "b", with the length of 1. 19 | ``` 20 | 21 | Example 3 22 | ``` 23 | Input: s = "pwwkew" 24 | Output: 3 25 | Explanation: The answer is "wke", with the length of 3. 26 | Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. 27 | ``` 28 | 29 | **constraints** 30 | 31 | - 0 <= s.length <= 5 * 10^4 32 | - `s` consists of English letters, digits, symbols and spaces. 33 | -------------------------------------------------------------------------------- /coding/longest_substring_without_repeating_characters/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_longest_substring_without_repeating_characers 3 | name: "Longest Substring Without Repeating Characters" 4 | category: coding 5 | subcategory: "string" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/longest_substring_without_repeating_characters/starter.c: -------------------------------------------------------------------------------- 1 | int lengthOfLongestSubstring(char * s){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/longest_substring_without_repeating_characters/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/longest_substring_without_repeating_characters/starter.go: -------------------------------------------------------------------------------- 1 | func lengthOfLongestSubstring(s string) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/longest_substring_without_repeating_characters/starter.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackerPen/tech-interview-questions/e6958bd582a586a7e28bfe9db7475565db0c2433/coding/longest_substring_without_repeating_characters/starter.java -------------------------------------------------------------------------------- /coding/longest_substring_without_repeating_characters/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} s 3 | * @return {number} 4 | */ 5 | var lengthOfLongestSubstring = function(s) { 6 | 7 | }; 8 | -------------------------------------------------------------------------------- /coding/longest_substring_without_repeating_characters/starter.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def lengthOfLongestSubstring(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | 8 | -------------------------------------------------------------------------------- /coding/longest_substring_without_repeating_characters/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {String} s 2 | # @return {Integer} 3 | def length_of_longest_substring(s) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/lru_cache/README_en.md: -------------------------------------------------------------------------------- 1 | ## LRU Cache 2 | 3 | Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. 4 | 5 | **get(key)** 6 | 7 | Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. 8 | 9 | 10 | **put(key, value)** 11 | 12 | Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item. 13 | 14 | ### Example 15 | 16 | ``` 17 | 18 | LRUCache cache = new LRUCache( 2 /* capacity */ ); 19 | 20 | cache.put(1, 1); 21 | cache.put(2, 2); 22 | cache.get(1); // returns 1 23 | cache.put(3, 3); // evicts key 2 24 | cache.get(2); // returns -1 (not found) 25 | cache.put(4, 4); // evicts key 1 26 | cache.get(1); // returns -1 (not found) 27 | cache.get(3); // returns 3 28 | cache.get(4); // returns 4 29 | ``` 30 | -------------------------------------------------------------------------------- /coding/lru_cache/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_lru_cache 3 | name: "LRU Cache" 4 | category: coding 5 | subcategory: "linked list" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | - finance 21 | - education 22 | - ecommerce 23 | - social_media 24 | - dev_tools 25 | -------------------------------------------------------------------------------- /coding/lru_cache/starter.c: -------------------------------------------------------------------------------- 1 | 2 | typedef struct { 3 | 4 | } LRUCache; 5 | 6 | 7 | LRUCache* lRUCacheCreate(int capacity) { 8 | 9 | } 10 | 11 | int lRUCacheGet(LRUCache* obj, int key) { 12 | 13 | } 14 | 15 | void lRUCachePut(LRUCache* obj, int key, int value) { 16 | 17 | } 18 | 19 | void lRUCacheFree(LRUCache* obj) { 20 | 21 | } 22 | 23 | /** 24 | * Your LRUCache struct will be instantiated and called as such: 25 | * LRUCache* obj = lRUCacheCreate(capacity); 26 | * int param_1 = lRUCacheGet(obj, key); 27 | 28 | * lRUCachePut(obj, key, value); 29 | 30 | * lRUCacheFree(obj); 31 | */ 32 | -------------------------------------------------------------------------------- /coding/lru_cache/starter.cpp: -------------------------------------------------------------------------------- 1 | class LRUCache { 2 | public: 3 | LRUCache(int capacity) { 4 | 5 | } 6 | 7 | int get(int key) { 8 | 9 | } 10 | 11 | void put(int key, int value) { 12 | 13 | } 14 | }; 15 | 16 | /** 17 | * Your LRUCache object will be instantiated and called as such: 18 | * LRUCache* obj = new LRUCache(capacity); 19 | * int param_1 = obj->get(key); 20 | * obj->put(key,value); 21 | */ 22 | -------------------------------------------------------------------------------- /coding/lru_cache/starter.go: -------------------------------------------------------------------------------- 1 | type LRUCache struct { 2 | 3 | } 4 | 5 | 6 | func Constructor(capacity int) LRUCache { 7 | 8 | } 9 | 10 | 11 | func (this *LRUCache) Get(key int) int { 12 | 13 | } 14 | 15 | 16 | func (this *LRUCache) Put(key int, value int) { 17 | 18 | } 19 | 20 | 21 | /** 22 | * Your LRUCache object will be instantiated and called as such: 23 | * obj := Constructor(capacity); 24 | * param_1 := obj.Get(key); 25 | * obj.Put(key,value); 26 | */ 27 | -------------------------------------------------------------------------------- /coding/lru_cache/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} capacity 3 | */ 4 | var LRUCache = function(capacity) { 5 | 6 | }; 7 | 8 | /** 9 | * @param {number} key 10 | * @return {number} 11 | */ 12 | LRUCache.prototype.get = function(key) { 13 | 14 | }; 15 | 16 | /** 17 | * @param {number} key 18 | * @param {number} value 19 | * @return {void} 20 | */ 21 | LRUCache.prototype.put = function(key, value) { 22 | 23 | }; 24 | 25 | /** 26 | * Your LRUCache object will be instantiated and called as such: 27 | * var obj = new LRUCache(capacity) 28 | * var param_1 = obj.get(key) 29 | * obj.put(key,value) 30 | */ 31 | -------------------------------------------------------------------------------- /coding/lru_cache/starter.py: -------------------------------------------------------------------------------- 1 | class LRUCache(object): 2 | 3 | def __init__(self, capacity): 4 | """ 5 | :type capacity: int 6 | """ 7 | 8 | 9 | def get(self, key): 10 | """ 11 | :type key: int 12 | :rtype: int 13 | """ 14 | 15 | 16 | def put(self, key, value): 17 | """ 18 | :type key: int 19 | :type value: int 20 | :rtype: None 21 | """ 22 | 23 | 24 | 25 | # Your LRUCache object will be instantiated and called as such: 26 | # obj = LRUCache(capacity) 27 | # param_1 = obj.get(key) 28 | # obj.put(key,value) 29 | -------------------------------------------------------------------------------- /coding/lru_cache/starter.rb: -------------------------------------------------------------------------------- 1 | class LRUCache 2 | 3 | =begin 4 | :type capacity: Integer 5 | =end 6 | def initialize(capacity) 7 | 8 | end 9 | 10 | 11 | =begin 12 | :type key: Integer 13 | :rtype: Integer 14 | =end 15 | def get(key) 16 | 17 | end 18 | 19 | 20 | =begin 21 | :type key: Integer 22 | :type value: Integer 23 | :rtype: Void 24 | =end 25 | def put(key, value) 26 | 27 | end 28 | 29 | 30 | end 31 | 32 | # Your LRUCache object will be instantiated and called as such: 33 | # obj = LRUCache.new(capacity) 34 | # param_1 = obj.get(key) 35 | # obj.put(key, value) 36 | -------------------------------------------------------------------------------- /coding/maximum_product_subarray/README_en.md: -------------------------------------------------------------------------------- 1 | ## Maximum Product Subarray 2 | 3 | Given an integer array `nums`, find a contiguous non-empty subarray within the array that has the largest product, and return the product. 4 | 5 | The test cases are generated so that the answer will fit in a **32-bit** integer. 6 | 7 | A **subarray** is a contiguous subsequence of the array. 8 | 9 | 10 | ### Examples 11 | 12 | Example 1 13 | ``` 14 | Input: nums = [2,3,-2,4] 15 | Output: 6 16 | Explanation: [2,3] has the largest product 6. 17 | ``` 18 | 19 | Example 2 20 | ``` 21 | Input: nums = [-2,0,-1] 22 | Output: 0 23 | Explanation: The result cannot be 2, because [-2,-1] is not a subarray. 24 | ``` 25 | 26 | **constraints:** 27 | 28 | - 1 <= nums.length <= 2 * 104 29 | - -10 <= nums[i] <= 10 30 | - The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. 31 | -------------------------------------------------------------------------------- /coding/maximum_product_subarray/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_maximum_product_subarray 3 | name: "Maximum Product Subarray" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/maximum_product_subarray/starter.c: -------------------------------------------------------------------------------- 1 | int maxProduct(int* nums, int numsSize){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/maximum_product_subarray/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& nums) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/maximum_product_subarray/starter.go: -------------------------------------------------------------------------------- 1 | func maxProduct(nums []int) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/maximum_product_subarray/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProduct(int[] nums) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/maximum_product_subarray/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var maxProduct = function(nums) { 6 | 7 | }; 8 | -------------------------------------------------------------------------------- /coding/maximum_product_subarray/starter.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maxProduct(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | 8 | -------------------------------------------------------------------------------- /coding/maximum_product_subarray/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @return {Integer} 3 | def max_product(nums) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/maximum_subarray/README_en.md: -------------------------------------------------------------------------------- 1 | ## Maximum Subarray 2 | 3 | Given an integer array `nums`, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. 4 | 5 | A subarray is a contiguous part of an array. 6 | 7 | ### Examples 8 | 9 | Example 1 10 | ``` 11 | Input: nums = [-2,1,-3,4,-1,2,1,-5,4] 12 | Output: 6 13 | 14 | Explanation: [4,-1,2,1] has the largest sum = 6. 15 | ``` 16 | 17 | Example 2 18 | ``` 19 | Input: nums = [1] 20 | Output: 1 21 | ``` 22 | 23 | Example 3 24 | ``` 25 | Input: nums = [5,4,-1,7,8] 26 | Output: 23 27 | ``` 28 | 29 | **constraints:** 30 | 31 | - 1 <= nums.length <= 105 32 | - -104 <= nums[i] <= 104 33 | -------------------------------------------------------------------------------- /coding/maximum_subarray/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for maximum subarray 2 | 3 | 4 | ### Approach 1: Brute Force 5 | 6 | **algorithm** 7 | 8 | - Initialize a variable `maxSubarray = -infinity` to keep track of the best subarray. We need to use negative infinity, not 0, because it is possible that there are only negative numbers in the array. 9 | - Use a for loop that considers each index of the array as a starting point. 10 | - For each starting point, create a variable currentSubarray = 0. Then, loop through the array from the starting index, adding each element to currentSubarray. Every time we add an element it represents a possible subarray - so continuously update maxSubarray to contain the maximum out of the currentSubarray and itself. 11 | - Return maxSubarray. 12 | 13 | **implementation** 14 | 15 | ```java 16 | class Solution { 17 | public int maxSubArray(int[] nums) { 18 | int maxSubarray = Integer.MIN_VALUE; 19 | for (int i = 0; i < nums.length; i++) { 20 | int currentSubarray = 0; 21 | for (int j = i; j < nums.length; j++) { 22 | currentSubarray += nums[j]; 23 | maxSubarray = Math.max(maxSubarray, currentSubarray); 24 | } 25 | } 26 | 27 | return maxSubarray; 28 | } 29 | } 30 | ``` 31 | 32 | **complexity analysis:** 33 | 34 | * Time complexity: `O(N^2)`, where `N` is the length of `nums`. We use 2 nested for loops, with each loop iterating through `nums`. 35 | * Space complexity: `O(1)`. No matter how big the input is, we are only ever using 2 variables: ans and currentSubarray. 36 | 37 | ### Approach 1: Dynamic Programming 38 | 39 | **algorithm** 40 | 41 | - Initialize 2 integer variables. Set both of them equal to the first value in the array. 42 | - `currentSubarray` will keep the running count of the current subarray we are focusing on. 43 | - `maxSubarray` will be our final return value. Continuously update it whenever we find a bigger subarray. 44 | - Iterate through the array, starting with the 2nd element (as we used the first element to initialize our variables). For each number, add it to the `currentSubarray` we are building. If `currentSubarray` becomes negative, we know it isn't worth keeping, so throw it away. Remember to update `maxSubarray` every time we find a new maximum. 45 | - Return maxSubarray. 46 | 47 | **implementation** 48 | 49 | ```java 50 | class Solution { 51 | public int maxSubArray(int[] nums) { 52 | // Initialize our variables using the first element. 53 | int currentSubarray = nums[0]; 54 | int maxSubarray = nums[0]; 55 | 56 | // Start with the 2nd element since we already used the first one. 57 | for (int i = 1; i < nums.length; i++) { 58 | int num = nums[i]; 59 | // If current_subarray is negative, throw it away. Otherwise, keep adding to it. 60 | currentSubarray = Math.max(num, currentSubarray + num); 61 | maxSubarray = Math.max(maxSubarray, currentSubarray); 62 | } 63 | 64 | return maxSubarray; 65 | } 66 | } 67 | ``` 68 | 69 | ```python 70 | class Solution: 71 | def maxSubArray(self, nums: List[int]) -> int: 72 | # Initialize our variables using the first element. 73 | current_subarray = max_subarray = nums[0] 74 | 75 | # Start with the 2nd element since we already used the first one. 76 | for num in nums[1:]: 77 | # If current_subarray is negative, throw it away. Otherwise, keep adding to it. 78 | current_subarray = max(num, current_subarray + num) 79 | max_subarray = max(max_subarray, current_subarray) 80 | 81 | return max_subarray 82 | ``` 83 | -------------------------------------------------------------------------------- /coding/maximum_subarray/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_maximum_subarray 3 | name: "Maximum Subarray" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: easy 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/maximum_subarray/starter.c: -------------------------------------------------------------------------------- 1 | int maxSubArray(int* nums, int numsSize){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/maximum_subarray/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSubArray(vector& nums) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/maximum_subarray/starter.go: -------------------------------------------------------------------------------- 1 | func maxSubArray(nums []int) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/maximum_subarray/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxSubArray(int[] nums) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/maximum_subarray/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var maxSubArray = function(nums) { 6 | 7 | }; 8 | -------------------------------------------------------------------------------- /coding/maximum_subarray/starter.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maxSubArray(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | 8 | -------------------------------------------------------------------------------- /coding/maximum_subarray/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @return {Integer} 3 | def max_sub_array(nums) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/meeting_rooms/README_en.md: -------------------------------------------------------------------------------- 1 | ## Meetings 2 | 3 | Given array of meeting times where each meeting time is a [start,end]. Return whether one person can attend all the meetings 4 | 5 | ### Examples 6 | 7 | Example 1 8 | ``` 9 | Input: intervals = [[0,30],[5,10],[15,20]] 10 | Output: false 11 | ``` 12 | 13 | Example 2 14 | ``` 15 | Input: intervals = [[7,10],[2,4]] 16 | Output: true 17 | ``` 18 | -------------------------------------------------------------------------------- /coding/meeting_rooms/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for Meetings 2 | 3 | 4 | ### Approach 1: Brute force 5 | 6 | **algorithm** 7 | 8 | compare every two meetings and see if there is overlap. 9 | 10 | **implementation** 11 | 12 | ```python 13 | class Solution: 14 | def canGoToMeetings(self, intervals: List[List[int]]) -> bool: 15 | if not intervals: 16 | return True 17 | n = len(intervals) 18 | for i in range(n-1): 19 | for j in range(i+1, n): 20 | if self.overlap(intervals[i], intervals[j]): 21 | return False 22 | return True 23 | def overlap(self, time1, time2) -> bool: 24 | start1, end1 = time1 25 | start2, end2 = time2 26 | return ((start1 >= start2 and start1 < end2) or (start2 >= start1 and start2 < end1)) 27 | ``` 28 | 29 | **complexity** 30 | 31 | - Time complexity: O(N^2) for N meetings. 32 | 33 | - Space complexity: O(1). No additional space used. 34 | 35 | ### Approach 2: Sort 36 | 37 | **algorithm** 38 | 39 | - Sort by start time 40 | - iterate through sorted meetings and check if current meeting starts after he previous one ends 41 | 42 | **implementation** 43 | 44 | ```python 45 | class Solution: 46 | def canGoToMeetings(self, intervals: List[List[int]]) -> bool: 47 | if not intervals: 48 | return True 49 | n = len(intervals) 50 | intervals.sort() 51 | previous_end = intervals[0][1] 52 | for i in range(1, n): 53 | start,end = intervals[i] 54 | if start < previous_end: 55 | return False 56 | previous_end = end 57 | return True 58 | ``` 59 | 60 | ```java 61 | class Solution { 62 | public boolean canGoToMeetings(int[][] intervals) { 63 | if (intervals.length < 2) return true; 64 | Arrays.sort(intervals, (a,b) -> a[0]-b[0]); 65 | int end = intervals[0][1]; 66 | for (int i = 1; i < intervals.length; i++){ 67 | if (intervals[i][0] < end) return false; 68 | end = intervals[i][1]; 69 | } 70 | return true; 71 | } 72 | } 73 | ``` 74 | 75 | ```javascript 76 | 77 | const canGoToMeetings = (intervals) => { 78 | if (intervals.length < 2) return true 79 | intervals.sort((a,b) => a[0]-b[0]) 80 | let previousEnd = intervals[0][1]; 81 | for (let i = 1; i < intervals.length; i++){ 82 | const [start,end] = intervals[i]; 83 | if (start < previousEnd) return false; 84 | previousEnd = end; 85 | } 86 | return true; 87 | } 88 | ``` 89 | **complexity** 90 | 91 | - Time complexity: O(N log N) for N meetings for sort algorithm. O(N) to iterate through sorted meetings once. 92 | 93 | - Space complexity: O(log N) for recursive stack space in sorting algorithm. O(1) for no additional space used. 94 | -------------------------------------------------------------------------------- /coding/meeting_rooms/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_meeting_rooms 3 | name: "Meeting Rooms" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: easy 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/meeting_rooms/starter.c: -------------------------------------------------------------------------------- 1 | bool canGoToMeetings(int** intervals, int intervalsSize, int* intervalsColSize){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/meeting_rooms/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canGoToMeetings(vector>& intervals) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/meeting_rooms/starter.go: -------------------------------------------------------------------------------- 1 | func canGoToMeetings(intervals [][]int) bool { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/meeting_rooms/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canGoToMeetings(int[][] intervals) { 3 | -------------------------------------------------------------------------------- /coding/meeting_rooms/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} intervals 3 | * @return {boolean} 4 | */ 5 | const canGoToMeetings = intervals => { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /coding/meeting_rooms/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - intervals: List[List[int]] 4 | # @return: bool 5 | def canGoToMeetings(self, intervals): 6 | -------------------------------------------------------------------------------- /coding/meeting_rooms/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[][]} intervals 2 | # @return {Boolean} 3 | def can_go_to_meetings(intervals) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/meeting_rooms_2/README_en.md: -------------------------------------------------------------------------------- 1 | ## Meeting Rooms 2 2 | 3 | Given an array of meeting times where every meeting time is a [start,end], return the minimum number of rooms required to host all meetings 4 | 5 | ### Examples 6 | 7 | Example 1 8 | 9 | ``` 10 | Input: intervals = [[0,30],[5,10],[15,20]] 11 | Output: 2 12 | ``` 13 | 14 | Example 2 15 | ``` 16 | Input: intervals = [[7,10],[2,4]] 17 | Output: 1 18 | ``` 19 | -------------------------------------------------------------------------------- /coding/meeting_rooms_2/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_meeting_rooms_2 3 | name: "Meeting Room 2" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/meeting_rooms_2/starter.c: -------------------------------------------------------------------------------- 1 | int minRooms(int** intervals, int intervalsSize, int* intervalsColSize){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/meeting_rooms_2/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minRooms(vector>& intervals) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/meeting_rooms_2/starter.go: -------------------------------------------------------------------------------- 1 | func minRooms(intervals [][]int) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/meeting_rooms_2/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minRooms(int[][] intervals) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/meeting_rooms_2/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} intervals 3 | * @return {number} 4 | */ 5 | const minRooms = (intervals) => { 6 | 7 | } 8 | -------------------------------------------------------------------------------- /coding/meeting_rooms_2/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - intervals: List[List[int]] 4 | # @return: int 5 | def minRooms(self, intervals): 6 | pass 7 | -------------------------------------------------------------------------------- /coding/meeting_rooms_2/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[][]} intervals 2 | # @return {Integer} 3 | def min_rooms(intervals) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/merge_intervals/README_en.md: -------------------------------------------------------------------------------- 1 | ## Merge Intervals 2 | 3 | Given an array of intervals where each intervals[i] = [start, end], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input. 4 | 5 | ### Examples 6 | 7 | Example 1 8 | ``` 9 | Input: intervals = [[1,3],[2,6],[8,10],[15,18]] 10 | Output: [[1,6],[8,10],[15,18]] 11 | Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6]. 12 | ``` 13 | 14 | Example 2 15 | ``` 16 | Input: intervals = [[1,4],[4,5]] 17 | Output: [[1,5]] 18 | Explanation: Intervals [1,4] and [4,5] are considered overlapping. 19 | ``` 20 | -------------------------------------------------------------------------------- /coding/merge_intervals/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for Merge Intervals 2 | 3 | ### Approach 1: Sorting 4 | 5 | **algorithm** 6 | 7 | If we sort intervals by start value, we can iterate through each interval knowing the one before is never starting after our current interval. So if our current interval start comes after the previous interval, we know they do not overlap. Otherwise, they overlap and we can merge them. When merging, make sure that we are taking the greater of the two ends. in the case of [[1,5], [2,3]], a merged interval will be [1,5] 8 | 9 | **implementation** 10 | 11 | ```python 12 | class Solution: 13 | def merge(self, intervals: List[List[int]]) -> List[List[int]]: 14 | if not intervals: return [] 15 | n = len(intervals) 16 | intervals.sort(key=lambda l:l[0]) 17 | merged = [intervals[0]] 18 | for i in range(1, n): 19 | start,end = intervals[i] 20 | if start <= merged[-1][1]: 21 | merged[-1][1] = max(merged[-1][1], end) 22 | else: 23 | merged.append(intervals[i]) 24 | return merged 25 | ``` 26 | 27 | ```javascript 28 | const merge = intervals => { 29 | if (intervals === null || intervals.length === 0) return []; 30 | const merged = []; 31 | intervals.sort((a, b) => a[0] - b[0]); 32 | for (const [start, end] of intervals) { 33 | let previousEnd = merged.length ? merged[merged.length - 1][1] : -Infinity; 34 | if (start <= previousEnd) { 35 | merged[merged.length - 1][1] = Math.max(end, previousEnd); 36 | } else { 37 | merged.push([start, end]); 38 | } 39 | } 40 | return merged; 41 | }; 42 | ``` 43 | 44 | **complexity** 45 | 46 | - Time complexity: O(N log N) for N intervals for sort algorithm. O(N) for linear scan of intervals. Overall O(N log N) will dominate. 47 | 48 | - Space complexity: O(log N) for recursive stack space of sort algorithm. Output array is not considered extra space here. 49 | -------------------------------------------------------------------------------- /coding/merge_intervals/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_merge_intervals 3 | name: "Merge Intervals" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | industries: 19 | -------------------------------------------------------------------------------- /coding/merge_intervals/starter.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | /** 4 | * Return an array of arrays of size *returnSize. 5 | * The sizes of the arrays are returned as *returnColumnSizes array. 6 | * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free(). 7 | */ 8 | int** merge(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize, int** returnColumnSizes){ 9 | 10 | } 11 | -------------------------------------------------------------------------------- /coding/merge_intervals/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> merge(vector>& intervals) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/merge_intervals/starter.go: -------------------------------------------------------------------------------- 1 | func merge(intervals [][]int) [][]int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/merge_intervals/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] merge(int[][] intervals) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/merge_intervals/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} intervals 3 | * @return {number[][]} 4 | */ 5 | 6 | const merge = intervals => { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /coding/merge_intervals/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - intervals: List[List[int] 4 | # @return: List[List[int]] 5 | def merge(self, intervals: List[List[int]]): 6 | pass 7 | -------------------------------------------------------------------------------- /coding/merge_intervals/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[][]} intervals 2 | # @return {Integer[][]} 3 | def merge(intervals) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/merge_k_sorted_lists/README_en.md: -------------------------------------------------------------------------------- 1 | ## Merge k Sorted Lists 2 | 3 | You are given an array of k linked-lists, each linked-list is sorted in ascending order. 4 | 5 | Merge all the linked-lists into one sorted linked-list and return it. 6 | 7 | ### Examples 8 | 9 | Example 1 10 | ``` 11 | Input: lists = [[1,4,5],[1,3,4],[2,6]] 12 | Output: [1,1,2,3,4,4,5,6] 13 | Explanation: The linked-lists are: 14 | [ 15 | 1->4->5, 16 | 1->3->4, 17 | 2->6 18 | ] 19 | merging them into one sorted list: 20 | 1->1->2->3->4->4->5->6 21 | ``` 22 | 23 | Example 2 24 | ``` 25 | Input: lists = [] 26 | Output: [] 27 | ``` 28 | -------------------------------------------------------------------------------- /coding/merge_k_sorted_lists/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_merge_k_sorted_lists 3 | name: "Merge K Sorted Lists" 4 | category: coding 5 | subcategory: "heap" 6 | difficulty: hard 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/merge_k_sorted_lists/starter.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HackerPen/tech-interview-questions/e6958bd582a586a7e28bfe9db7475565db0c2433/coding/merge_k_sorted_lists/starter.c -------------------------------------------------------------------------------- /coding/merge_k_sorted_lists/starter.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* mergeKLists(vector& lists) { 14 | 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /coding/merge_k_sorted_lists/starter.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func mergeKLists(lists []*ListNode) *ListNode { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /coding/merge_k_sorted_lists/starter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode() {} 7 | * ListNode(int val) { this.val = val; } 8 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | * } 10 | */ 11 | class Solution { 12 | public ListNode mergeKLists(ListNode[] lists) { 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /coding/merge_k_sorted_lists/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | /** 9 | * @param {ListNode[]} lists 10 | * @return {ListNode} 11 | */ 12 | 13 | const mergeKLists = lists => { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /coding/merge_k_sorted_lists/starter.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | # @params: 8 | # - lists: List[Optional[ListNode] 9 | # @return: Optional[ListNode] 10 | def mergeKLists(self, lists): 11 | pass 12 | -------------------------------------------------------------------------------- /coding/merge_k_sorted_lists/starter.rb: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode 3 | # attr_accessor :val, :next 4 | # def initialize(val = 0, _next = nil) 5 | # @val = val 6 | # @next = _next 7 | # end 8 | # end 9 | # @param {ListNode[]} lists 10 | # @return {ListNode} 11 | def merge_k_lists(lists) 12 | 13 | end 14 | -------------------------------------------------------------------------------- /coding/merge_two_sorted_linked_lists/README_en.md: -------------------------------------------------------------------------------- 1 | ## Merge Two Sorted Linked Lists 2 | 3 | Given two sorted linked lists, merge the two lists into one sorted list 4 | Return the merged linked list 5 | 6 | 7 | ### Examples 8 | 9 | Example 1 10 | ``` 11 | Input: list1 = [1,2,4], list2 = [1,3,4] 12 | Output: [1,1,2,3,4,4] 13 | ``` 14 | 15 | Example 2 16 | ``` 17 | Input: list1 = [], list2 = [] 18 | Output: [] 19 | ``` 20 | 21 | Example 3: 22 | 23 | ``` 24 | Input: list1 = [], list2 = [0] 25 | Output: [0] 26 | ``` 27 | -------------------------------------------------------------------------------- /coding/merge_two_sorted_linked_lists/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for Merge Two Sorted Linked Lists 2 | 3 | 4 | ### Approach 1: Recursive 5 | 6 | **algorithm** 7 | 8 | Base case is when either list is null. Return the non null list. 9 | Else, find the smaller value at the heads of both lists. This will be the next value of the merged list. Recursively call on the next node and the other list. 10 | 11 | **implementation** 12 | 13 | ```javascript 14 | const mergeTwoLists = (l1,l2) => { 15 | if (l1 === null) return l2; 16 | if (l2 === null) return l1; 17 | 18 | let cur; 19 | if (l1.val <= l2.val){ 20 | cur = l1; 21 | cur.next = mergeTwoLists(l1.next, l2); 22 | } else { 23 | cur = l2; 24 | cur.next = mergeTwoLists(l1, l2.next); 25 | } 26 | 27 | return cur; 28 | } 29 | ``` 30 | 31 | **complexity** 32 | 33 | * Time complexity: O(m+n) for lists of length m and n. There will be one recursive calls per node in both lists 34 | 35 | * Space complexity: O(m+n) Recursive call stack will be one for each node. So m+n total calls 36 | 37 | 38 | ### Approach 2: Iteration 39 | 40 | **algorithm** 41 | 42 | - set up a sentinel node to start the merged list 43 | - while both lists are not null, compare each head of list to see which is smaller and set next to smaller value. 44 | - increment pointers to move ahead in the linked list 45 | - at most one linked list will not be null. Connect remaining non null linked list 46 | 47 | **implementation** 48 | 49 | ```python 50 | class Solution: 51 | def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: 52 | if not list1: return list2 53 | if not list2: return list1 54 | sentinel = ListNode(-1) 55 | cur = sentinel 56 | while list1 and list2: 57 | if list1.val <= list2.val: 58 | cur.next = list1 59 | list1 = list1.next 60 | else: 61 | cur.next = list2 62 | list2 = list2.next 63 | cur = cur.next 64 | if not list1: cur.next = list2 65 | if not list2: cur.next = list1 66 | 67 | return sentinel.next 68 | ``` 69 | 70 | ```java 71 | class Solution { 72 | public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 73 | ListNode sentinel = new ListNode(-1); 74 | ListNode prev = sentinel; 75 | 76 | while (l1 != null && l2 != null){ 77 | if (l1.val <= l2.val){ 78 | prev.next = l1; 79 | l1 = l1.next; 80 | } else { 81 | prev.next = l2; 82 | l2 = l2.next; 83 | } 84 | prev = prev.next; 85 | } 86 | prev.next = l1 == null ? l2 : l1; 87 | 88 | return sentinel.next; 89 | } 90 | } 91 | ``` 92 | 93 | ```javascript 94 | const mergeTwoLists = (l1,l2) => { 95 | if (l1 === null) return l2; 96 | if (l2 === null) return l1; 97 | const sentinel = new ListNode(null); 98 | let cur = sentinel; 99 | while (l1 && l2){ 100 | if (l1.val <= l2.val){ 101 | cur.next = l1; 102 | l1 = l1.next; 103 | } else { 104 | cur.next = l2; 105 | l2 = l2.next; 106 | } 107 | cur = cur.next; 108 | } 109 | if (l1 === null) cur.next = l2; 110 | if (l2 === null) cur.next = l1; 111 | 112 | return sentinel.next; 113 | } 114 | ``` 115 | 116 | **complexity** 117 | 118 | * Time complexity: O(m+n) each node is traversed once. 119 | 120 | * Space complexity: O(1) for pointers 121 | -------------------------------------------------------------------------------- /coding/merge_two_sorted_linked_lists/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_merge_two_sorted_linked_lists 3 | name: "Merge Two Sorted Linked Lists" 4 | category: coding 5 | subcategory: "linked list" 6 | difficulty: easy 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/merge_two_sorted_linked_lists/starter.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * struct ListNode *next; 6 | * }; 7 | */ 8 | 9 | 10 | struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){ 11 | 12 | } 13 | -------------------------------------------------------------------------------- /coding/merge_two_sorted_linked_lists/starter.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { 14 | 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /coding/merge_two_sorted_linked_lists/starter.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /coding/merge_two_sorted_linked_lists/starter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode() {} 7 | * ListNode(int val) { this.val = val; } 8 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | * } 10 | */ 11 | class Solution { 12 | public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /coding/merge_two_sorted_linked_lists/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | /** 9 | * @param {ListNode} l1 10 | * @param {ListNode} l2 11 | * @return {ListNode} 12 | */ 13 | const mergeTwoLists = (l1, l2) => { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /coding/merge_two_sorted_linked_lists/starter.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | # @params: 8 | # - list1: Optional[ListNode] 9 | # - list2: Optional[ListNode] 10 | # @return: Optional[ListNode] 11 | def mergeTwoLists(self, list1, list2): 12 | pass 13 | -------------------------------------------------------------------------------- /coding/merge_two_sorted_linked_lists/starter.rb: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode 3 | # attr_accessor :val, :next 4 | # def initialize(val = 0, _next = nil) 5 | # @val = val 6 | # @next = _next 7 | # end 8 | # end 9 | # @param {ListNode} list1 10 | # @param {ListNode} list2 11 | # @return {ListNode} 12 | def merge_two_lists(list1, list2) 13 | 14 | end 15 | -------------------------------------------------------------------------------- /coding/missing_number/README_en.md: -------------------------------------------------------------------------------- 1 | ## Missing Number 2 | 3 | Given an array `nums` containing n distinct numbers in the range [0, n], return the only number in the range that is **missing** from the array. 4 | 5 | ### Examples 6 | 7 | Example 1 8 | ``` 9 | Input: nums = [3,0,1] 10 | Output: 2 11 | Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums. 12 | ``` 13 | 14 | Example 2 15 | ``` 16 | Input: nums = [0,1] 17 | Output: 2 18 | Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums. 19 | ``` 20 | 21 | Example 3 22 | ``` 23 | Input: nums = [9,6,4,2,3,5,7,0,1] 24 | Output: 8 25 | Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums. 26 | ``` 27 | 28 | **constraints** 29 | 30 | - n == nums.length 31 | - 1 <= n <= 100 32 | - 0 <= nums[i] <= n 33 | - All the numbers of nums are unique. 34 | -------------------------------------------------------------------------------- /coding/missing_number/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for Missing Number 2 | 3 | 4 | ### Approach 1: Sorting 5 | 6 | **algorithm** 7 | 8 | If `nums` were in order, it would be easy to see which number is missing. 9 | 10 | First, we sort nums. Then, we check the two special cases that can be handled in constant time - ensuring that 0 is at the beginning and that n is at the end. Given that those assumptions hold, the missing number must somewhere between (but not including) 0 and n. To find it, we ensure that the number we expect to be at each index is indeed there. Because we handled the edge cases, this is simply the previous number plus 1. Thus, as soon as we find an unexpected number, we can simply return the expected number. 11 | 12 | 13 | **implementation** 14 | 15 | ```python 16 | class Solution: 17 | def missingNumber(self, nums): 18 | nums.sort() 19 | 20 | # Ensure that n is at the last index 21 | if nums[-1] != len(nums): 22 | return len(nums) 23 | # Ensure that 0 is at the first index 24 | elif nums[0] != 0: 25 | return 0 26 | 27 | # If we get here, then the missing number is on the range (0, n) 28 | for i in range(1, len(nums)): 29 | expected_num = nums[i-1] + 1 30 | if nums[i] != expected_num: 31 | return expected_num 32 | ``` 33 | 34 | **complexity** 35 | 36 | Time complexity: `O(NlogN)`. The only elements of the algorithm that have asymptotically nonconstant time complexity are the main for loop (which runs in O(N) time), and the sort invocation (which runs in O(NlogN) time for Python and Java). Therefore, the runtime is dominated by sort, and the entire runtime is O(NlogN). 37 | 38 | Space complexity: `O(1) or O(N)`. In the sample code, we sorted `nums` in place, allowing us to avoid allocating additional space. If modifying nums is forbidden, we can allocate an `O(N)` size copy and sort that instead. 39 | 40 | ### Approach 2: HashSet 41 | 42 | **algorithm** 43 | 44 | A brute force method for solving this problem would be to simply check for the presence of each number that we expect to be present. The naive implementation might use a linear scan of the array to check for containment, but we can use a HashSet to get constant time containment queries and overall linear runtime. 45 | 46 | **implementation** 47 | 48 | ```python 49 | class Solution: 50 | def missingNumber(self, nums): 51 | num_set = set(nums) 52 | n = len(nums) + 1 53 | for number in range(n): 54 | if number not in num_set: 55 | return number 56 | ``` 57 | 58 | **complexity** 59 | 60 | Time complexity: `O(n)`. Because the set allows for O(1) containment queries, the main loop runs in O(n) time. Creating num_set costs O(n) time, as each set insertion runs in amortized O(1) time, so the overall runtime is O(n+n)=O(n). 61 | 62 | Space complexity: `O(N)`. `nums` contains `n−1` distinct elements, so it costs `O(n)` space to store a set containing all of them. 63 | -------------------------------------------------------------------------------- /coding/missing_number/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_missing_number 3 | name: "Missing Number" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: easy 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/missing_number/starter.c: -------------------------------------------------------------------------------- 1 | int missingNumber(int* nums, int numsSize){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/missing_number/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int missingNumber(vector& nums) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/missing_number/starter.go: -------------------------------------------------------------------------------- 1 | func missingNumber(nums []int) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/missing_number/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int missingNumber(int[] nums) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/missing_number/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var missingNumber = function(nums) { 6 | 7 | }; 8 | -------------------------------------------------------------------------------- /coding/missing_number/starter.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def missingNumber(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | 8 | -------------------------------------------------------------------------------- /coding/missing_number/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @return {Integer} 3 | def missing_number(nums) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/number_of_1_bits/README_en.md: -------------------------------------------------------------------------------- 1 | ## Number of 1 Bits 2 | 3 | Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight). 4 | 5 | Note: 6 | - Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned. 7 | - In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3, the input represents the signed integer. -3. 8 | 9 | ### Examples 10 | 11 | Example 1 12 | ``` 13 | Input: n = 00000000000000000000000000001011 14 | Output: 3 15 | Explanation: The input binary string 00000000000000000000000000001011 has a total of three '1' bits. 16 | ``` 17 | 18 | Example 2 19 | ``` 20 | Input: n = 00000000000000000000000010000000 21 | Output: 1 22 | Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit. 23 | ``` 24 | 25 | Example 3 26 | ``` 27 | Input: n = 11111111111111111111111111111101 28 | Output: 31 29 | Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits. 30 | ``` 31 | 32 | **constraints**: 33 | 34 | - The input must be a **binary string** of length 32. 35 | -------------------------------------------------------------------------------- /coding/number_of_1_bits/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for Number of 1 Bits 2 | 3 | 4 | ### Approach 1: Loop and Flip 5 | 6 | **algorithm** 7 | 8 | The solution is straight-forward. We check each of the 32 bits of the number. If the bit is 1, we add one to the number of 1-bits. 9 | 10 | We can check the `i_th` bit of a number using a bit mask. We start with a mask m=1, because the binary representation of 1 is, `000100000000000000000000000000000001` Clearly, a logical AND between any number and the mask 1 gives us the least significant bit of this number. To check the next bit, we shift the mask to the left by one. 11 | 12 | `001000000000000000000000000000000010` 13 | 14 | And so on. 15 | 16 | **implementation** 17 | 18 | ```java 19 | public int hammingWeight(int n) { 20 | int bits = 0; 21 | int mask = 1; 22 | for (int i = 0; i < 32; i++) { 23 | if ((n & mask) != 0) { 24 | bits++; 25 | } 26 | mask <<= 1; 27 | } 28 | return bits; 29 | } 30 | ``` 31 | 32 | **complexity** 33 | 34 | Time complexity: `O(1)`. Because n in this piece of code is a 32-bit integer, the time complexity is O(1). 35 | 36 | Space complexity: `O(1)`. No additional space is allocated. 37 | 38 | ### Approach 2: Bit Manipulation 39 | 40 | **algorithm** 41 | 42 | We can make the previous algorithm simpler and a little faster. Instead of checking every bit of the number, we repeatedly flip the least-significant 1-bit of the number to 0, and add 1 to the sum. As soon as the number becomes 0, we know that it does not have any more 1-bits, and we return the sum. 43 | 44 | The key idea here is to realize that for any number n, doing a bit-wise AND of n and n−1 flips the least-significant 1-bit in n to 0. Why? Consider the binary representations of n and n - 1. In the binary representation, the least significant 1-bit in n always corresponds to a 0-bit in n−1. Therefore, anding the two numbers n and n−1 always flips the least significant 1-bit in n to 0, and keeps all other bits the same. 45 | 46 | Using this trick, the code becomes very simple. 47 | 48 | **implementation** 49 | 50 | ```java 51 | public int hammingWeight(int n) { 52 | int sum = 0; 53 | while (n != 0) { 54 | sum++; 55 | n &= (n - 1); 56 | } 57 | return sum; 58 | } 59 | ``` 60 | 61 | **complexity** 62 | 63 | Time complexity: `O(1)`. The run time depends on the number of 1-bits in n. In the worst case, all bits in n are 1-bits 64 | 65 | Space complexity: `O(1)`. No additional space is allocated. 66 | -------------------------------------------------------------------------------- /coding/number_of_1_bits/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_number_of_1_bits 3 | name: "Number of 1 bits" 4 | category: coding 5 | subcategory: "binary" 6 | difficulty: easy 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/number_of_1_bits/starter.c: -------------------------------------------------------------------------------- 1 | int hammingWeight(uint32_t n) { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/number_of_1_bits/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int hammingWeight(uint32_t n) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/number_of_1_bits/starter.go: -------------------------------------------------------------------------------- 1 | func hammingWeight(num uint32) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/number_of_1_bits/starter.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need to treat n as an unsigned value 3 | public int hammingWeight(int n) { 4 | 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /coding/number_of_1_bits/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n - a positive integer 3 | * @return {number} 4 | */ 5 | var hammingWeight = function(n) { 6 | 7 | }; 8 | -------------------------------------------------------------------------------- /coding/number_of_1_bits/starter.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def hammingWeight(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | 8 | -------------------------------------------------------------------------------- /coding/number_of_1_bits/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer} n, a positive integer 2 | # @return {Integer} 3 | def hamming_weight(n) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/number_of_islands/README_en.md: -------------------------------------------------------------------------------- 1 | ## Number of Islands 2 | 3 | Given an m x n grid which represents a map of '1's (land) and '0's (water), return the number of islands. 4 | 5 | An island is surrouned by water. Assume the grid is surrounded by water. Land can be connected either horizontally or vertically. 6 | 7 | ### Examples 8 | 9 | Example 1 10 | ``` 11 | Input: grid = [ 12 | ["1","1","1","1","0"], 13 | ["1","1","0","1","0"], 14 | ["1","1","0","0","0"], 15 | ["0","0","0","0","0"] 16 | ] 17 | Output: 1 18 | ``` 19 | 20 | Example 2 21 | ``` 22 | Input: grid = [ 23 | ["1","1","0","0","0"], 24 | ["1","1","0","0","0"], 25 | ["0","0","1","0","0"], 26 | ["0","0","0","1","1"] 27 | ] 28 | Output: 3 29 | ``` 30 | -------------------------------------------------------------------------------- /coding/number_of_islands/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for Number of Islands 2 | 3 | 4 | ### Approach 1: DFS 5 | 6 | **algorithm** 7 | 8 | The grid is an undirected graph. Every "1" cell has an edge if it is connected to another "1" cell in one of four directions. Iterate through the grid. At every "1", depth first search in 4 directions and mark connected cells to avoid double counting them. 9 | - if you cannot modify the input, a visited 2D array will be needed to mark cells as visited or seen. This will incur extra space O(m*n) for m x n grid. 10 | - if you can modify, we can turn "1" to "0" and leave them marked 11 | At every "1" after we mark connected land, we can increment an islands counter 12 | 13 | **implementation** 14 | 15 | ```python 16 | class Solution: 17 | def countIslands(self, grid: List[List[str]]) -> int: 18 | if not grid: 19 | return 0 20 | self.grid = grid 21 | self.m, self.n = len(grid), len(grid[0]) 22 | self.directions = ((1,0),(0,1),(-1,0),(0,-1)) 23 | islands = 0 24 | for i in range(self.m): 25 | for j in range(self.n): 26 | if grid[i][j] == "1": 27 | self.markLands(i,j) 28 | islands += 1 29 | 30 | return islands 31 | 32 | def markLands(self, i: int, j: int): 33 | self.grid[i][j] = "0" 34 | for di,dj in self.directions: 35 | newi, newj = i + di, j + dj 36 | if self.isValid(newi,newj): 37 | self.markLands(newi,newj) 38 | 39 | def isValid(self, i: int, j: int) -> bool: 40 | return (i >= 0 and i < self.m and j >= 0 and j < self.n and self.grid[i][j] == "1") 41 | ``` 42 | 43 | ```javascript 44 | const countIslands = (grid) => { 45 | if (grid === null || grid.length === 0) return 0; 46 | const m = grid.length, n = grid[0].length; 47 | const isValid = (i,j) => (i >= 0 && i < m && j >= 0 && j < n && grid[i][j] === "1"); 48 | const directions = [[0,1],[1,0],[-1,0],[0,-1]]; 49 | 50 | const markLand = (i,j) => { 51 | grid[i][j] = "0"; 52 | for (const [di,dj] of directions){ 53 | const newI = i + di, newJ = j + dj; 54 | if (isValid(newI,newJ)){ 55 | markLand(newI,newJ); 56 | } 57 | } 58 | } 59 | 60 | let islands = 0; 61 | for (let i = 0; i < m; i++){ 62 | for (let j = 0; j < n; j++){ 63 | if (grid[i][j] === "1"){ 64 | markLand(i,j); 65 | islands++; 66 | } 67 | } 68 | } 69 | return islands; 70 | } 71 | ``` 72 | **complexity** 73 | 74 | - Time complexity: O(M*N) for M x N grid. 75 | 76 | - Space complexity: O(M*N) recursive stack space required in the worst case. 77 | 78 | ### Additional approaches: Both BFS and Union Find will perform in similar time and space complexity 79 | -------------------------------------------------------------------------------- /coding/number_of_islands/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_number_of_islands 3 | name: "Number of Islands" 4 | category: coding 5 | subcategory: "graph" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/number_of_islands/starter.c: -------------------------------------------------------------------------------- 1 | int countIslands(char** grid, int gridSize, int* gridColSize){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/number_of_islands/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countIslands(vector>& grid) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/number_of_islands/starter.go: -------------------------------------------------------------------------------- 1 | func countIslands(grid [][]byte) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/number_of_islands/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countIslands(char[][] grid) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/number_of_islands/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {character[][]} grid 3 | * @return {number} 4 | */ 5 | 6 | const countIslands = (grid) => { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /coding/number_of_islands/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - grid: List[List[str]] 4 | # @return: int 5 | def countIslands(self, grid): 6 | pass 7 | -------------------------------------------------------------------------------- /coding/number_of_islands/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Character[][]} grid 2 | # @return {Integer} 3 | def count_islands(grid) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/product_of_array_except_self/README_en.md: -------------------------------------------------------------------------------- 1 | ## Product of Array Except Self 2 | 3 | Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. 4 | The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. 5 | You must write an algorithm that runs in O(n) time and without using the division operation. 6 | 7 | 8 | 9 | Example 1: 10 | ``` 11 | Input: nums = [1,2,3,4] 12 | Output: [24,12,8,6] 13 | ``` 14 | 15 | Example 2: 16 | ``` 17 | Input: nums = [-1,1,0,-3,3] 18 | Output: [0,0,9,0,0] 19 | ``` 20 | 21 | **Constraints**: 22 | 23 | - 2 <= nums.length <= 105 24 | - -30 <= nums[i] <= 30 25 | - The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. 26 | 27 | **Follow up:** Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.) 28 | -------------------------------------------------------------------------------- /coding/product_of_array_except_self/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_product_of_array_except_self 3 | name: "Product of Array except self" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/product_of_array_except_self/starter.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Note: The returned array must be malloced, assume caller calls free(). 3 | */ 4 | int* productExceptSelf(int* nums, int numsSize, int* returnSize){ 5 | 6 | } 7 | -------------------------------------------------------------------------------- /coding/product_of_array_except_self/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector productExceptSelf(vector& nums) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/product_of_array_except_self/starter.go: -------------------------------------------------------------------------------- 1 | func productExceptSelf(nums []int) []int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/product_of_array_except_self/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] productExceptSelf(int[] nums) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/product_of_array_except_self/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number[]} 4 | */ 5 | var productExceptSelf = function(nums) { 6 | 7 | }; 8 | -------------------------------------------------------------------------------- /coding/product_of_array_except_self/starter.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def productExceptSelf(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: List[int] 6 | """ 7 | 8 | -------------------------------------------------------------------------------- /coding/product_of_array_except_self/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @return {Integer[]} 3 | def product_except_self(nums) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/reverse_bits/README_en.md: -------------------------------------------------------------------------------- 1 | ## Reverse Bits 2 | 3 | Reverse bits of a given 32 bits unsigned integer. 4 | 5 | Note: 6 | - Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned. 7 | - In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 2 above, the input represents the signed integer -3 and the output represents the signed integer `-1073741825`. 8 | 9 | ### Examples 10 | 11 | Example 1 12 | ``` 13 | Input: n = 00000010100101000001111010011100 14 | Output: 964176192 (00111001011110000010100101000000) 15 | Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000. 16 | ``` 17 | 18 | Example 2 19 | ``` 20 | Input: n = 11111111111111111111111111111101 21 | Output: 3221225471 (10111111111111111111111111111111) 22 | Explanation: The input binary string 11111111111111111111111111111101 represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is 10111111111111111111111111111111. 23 | ``` 24 | 25 | **constraints** 26 | 27 | - The input must be a **binary string** of length 32 28 | -------------------------------------------------------------------------------- /coding/reverse_bits/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for Reverse Bit 2 | 3 | ### Approach 1: Bit by Bit 4 | 5 | **algorithm** 6 | 7 | Though the question is not difficult, it often serves as a warm-up question to kick off the interview. The point is to test one's basic knowledge on data type and bit operations. 8 | 9 | We iterate through the bit string of the input integer, from right to left (i.e. n = n >> 1). To retrieve the right-most bit of an integer, we apply the bit AND operation `(n & 1)`. 10 | 11 | For each bit, we reverse it to the correct position (i.e. (n & 1) << power). Then we accumulate this reversed bit to the final result. 12 | 13 | When there is no more bits of one left (i.e. n == 0), we terminate the iteration. 14 | 15 | **implementation** 16 | 17 | ```python 18 | class Solution: 19 | # @param n, an integer 20 | # @return an integer 21 | def reverseBits(self, n): 22 | ret, power = 0, 31 23 | while n: 24 | ret += (n & 1) << power 25 | n = n >> 1 26 | power -= 1 27 | return ret 28 | ``` 29 | 30 | ```go 31 | func reverseBits(num uint32) uint32 { 32 | ret := uint32(0) 33 | power := uint32(31) 34 | for num != 0 { 35 | ret += (num & 1) << power 36 | num = num >> 1 37 | power -= 1 38 | } 39 | return ret 40 | } 41 | ``` 42 | 43 | ```cpp 44 | class Solution { 45 | public: 46 | uint32_t reverseBits(uint32_t n) { 47 | uint32_t ret = 0, power = 31; 48 | while (n != 0) { 49 | ret += (n & 1) << power; 50 | n = n >> 1; 51 | power -= 1; 52 | } 53 | return ret; 54 | } 55 | }; 56 | ``` 57 | 58 | **complexity** 59 | 60 | Time complexity: `O(1)`. Though we have a loop in the algorithm, the number of iteration is fixed regardless the input, since the integer is of fixed-size (32-bits) in our problem. 61 | 62 | Space complexity: `O(1)`. The consumption of memory is constant regardless the input. 63 | -------------------------------------------------------------------------------- /coding/reverse_bits/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_reverse_bits 3 | name: "Reverse Bits" 4 | category: coding 5 | subcategory: "binary" 6 | difficulty: easy 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/reverse_bits/starter.c: -------------------------------------------------------------------------------- 1 | uint32_t reverseBits(uint32_t n) { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/reverse_bits/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | uint32_t reverseBits(uint32_t n) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/reverse_bits/starter.go: -------------------------------------------------------------------------------- 1 | func reverseBits(num uint32) uint32 { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/reverse_bits/starter.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need treat n as an unsigned value 3 | public int reverseBits(int n) { 4 | 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /coding/reverse_bits/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n - a positive integer 3 | * @return {number} - a positive integer 4 | */ 5 | var reverseBits = function(n) { 6 | 7 | }; 8 | -------------------------------------------------------------------------------- /coding/reverse_bits/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @param n, an integer 3 | # @return an integer 4 | def reverseBits(self, n): 5 | 6 | -------------------------------------------------------------------------------- /coding/reverse_bits/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer} n, a positive integer 2 | # @return {Integer} 3 | def reverse_bits(n) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/reverse_linked_list/README_en.md: -------------------------------------------------------------------------------- 1 | ## Reverse Linked List 2 | 3 | Given a Linked List, reverse it and return it. 4 | 5 | ### Examples 6 | 7 | Example 1 8 | ``` 9 | Input: head = [1,2,3,4,5] 10 | Output: [5,4,3,2,1] 11 | ``` 12 | 13 | Example 2 14 | ``` 15 | Input: head = [1,2] 16 | Output: [2,1] 17 | ``` 18 | 19 | Example 3: 20 | ``` 21 | Input: head = [] 22 | Output: [] 23 | `````` 24 | -------------------------------------------------------------------------------- /coding/reverse_linked_list/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for Reverse Linked List 2 | 3 | ### Approach 1: Iterate 4 | 5 | **algorithm** 6 | 7 | While you traverse the linked list, change current node's next pointer to point to the previous node. 8 | Keep track of next node with a pointer. Store reference to previous node with a pointer. "Walk" forward making sure you move previous pointer to current node and then current node to the next node. 9 | 10 | **implementation** 11 | 12 | ```python 13 | class Solution: 14 | def reverseLinkedList(self, head: Optional[ListNode]) -> Optional[ListNode]: 15 | if not head: 16 | return head 17 | cur, pre = head, None 18 | while cur: 19 | nextNode = cur.next 20 | cur.next = pre 21 | pre = cur 22 | cur = nextNode 23 | return pre 24 | ``` 25 | 26 | ```java 27 | class Solution { 28 | public ListNode reverseLinkedList(ListNode head) { 29 | ListNode cur = head; 30 | ListNode pre = null; 31 | while (cur != null){ 32 | ListNode nextNode = cur.next; 33 | cur.next = pre; 34 | pre = cur; 35 | cur = nextNode; 36 | } 37 | return pre; 38 | } 39 | } 40 | ``` 41 | 42 | ```javascript 43 | const reverseLinkedList = head => { 44 | if (head === null) return head; 45 | let cur = head, 46 | pre = null; 47 | while (cur !== null) { 48 | const nextNode = cur.next; 49 | cur.next = pre; 50 | pre = cur; 51 | cur = nextNode; 52 | } 53 | return pre; 54 | }; 55 | ``` 56 | 57 | **complexity** 58 | 59 | - Time complexity: O(N) for N nodes in linked list 60 | 61 | - Space complexity: O(1) for pointers 62 | -------------------------------------------------------------------------------- /coding/reverse_linked_list/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_reverse_linked_list 3 | name: "Reverse Linked List" 4 | category: coding 5 | subcategory: "linked list" 6 | difficulty: easy 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/reverse_linked_list/starter.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * struct ListNode *next; 6 | * }; 7 | */ 8 | 9 | 10 | struct ListNode* reverseLinkedList(struct ListNode* head){ 11 | 12 | } 13 | -------------------------------------------------------------------------------- /coding/reverse_linked_list/starter.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | ListNode* reverseLinkedList(ListNode* head) { 14 | 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /coding/reverse_linked_list/starter.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func reverseLinkedList(head *ListNode) *ListNode { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /coding/reverse_linked_list/starter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode() {} 7 | * ListNode(int val) { this.val = val; } 8 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | * } 10 | */ 11 | class Solution { 12 | public ListNode reverseLinkedList(ListNode head) { 13 | 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /coding/reverse_linked_list/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * function ListNode(val) { 4 | * this.val = val; 5 | * this.next = null; 6 | * } 7 | */ 8 | /** 9 | * @param {ListNode} head 10 | * @return {ListNode} 11 | */ 12 | 13 | const reverseLinkedList = head => { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /coding/reverse_linked_list/starter.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | # @params 8 | # - head: Optional[ListNode] 9 | # @return: Optional[ListNode] 10 | def reverseLinkedList(self, head): 11 | pass 12 | -------------------------------------------------------------------------------- /coding/reverse_linked_list/starter.rb: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode 3 | # attr_accessor :val, :next 4 | # def initialize(val = 0, _next = nil) 5 | # @val = val 6 | # @next = _next 7 | # end 8 | # end 9 | # @param {ListNode} head 10 | # @return {ListNode} 11 | def reverse_linked_list(head) 12 | 13 | end 14 | -------------------------------------------------------------------------------- /coding/rotate_matrix/README_en.md: -------------------------------------------------------------------------------- 1 | ## Rotate Matrix 2 | 3 | Given n x n 2D matrix, rotate the matrix by 90 degrees clockwise. Rotate in place. 4 | 5 | ### Examples 6 | 7 | Example 1 8 | ``` 9 | Input: matrix = [ 10 | [1,2,3], 11 | [4,5,6], 12 | [7,8,9] 13 | ] 14 | Output: [ 15 | [7,4,1], 16 | [8,5,2], 17 | [9,6,3] 18 | ] 19 | ``` 20 | 21 | Example 2 22 | ``` 23 | Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] 24 | Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] 25 | ``` 26 | -------------------------------------------------------------------------------- /coding/rotate_matrix/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for Rotate Matrix 2 | 3 | ### Approach 1: Reverse on diagonal and reverse left to right 4 | 5 | **algorithm** 6 | transpose and reflect: 7 | - reverse across the diagonal 8 | - reverse each row 9 | 10 | 11 | **implementation** 12 | 13 | ```python 14 | class Solution: 15 | def rotate(self, matrix: List[List[int]]) -> None: 16 | self.transpose(matrix) 17 | self.reflect(matrix) 18 | 19 | def transpose(self, matrix): 20 | n = len(matrix) 21 | for i in range(n): 22 | for j in range(i, n): 23 | matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] 24 | 25 | def reflect(self, matrix): 26 | n = len(matrix) 27 | for i in range(n): 28 | matrix[i].reverse() 29 | ``` 30 | 31 | ```javascript 32 | const swap = (i,j, arr) => [arr[i][j],arr[j][i]] = [arr[j][i],arr[i][j]]; 33 | const rotate = (matrix) => { 34 | if (matrix === null || matrix.length === 0) return; 35 | const n = matrix.length; 36 | for (let i = 0; i < n; i++){ 37 | for (let j = i; j < n; j++){ 38 | swap(i,j, matrix) 39 | } 40 | } 41 | for (let i = 0; i < n; i++){ 42 | matrix[i].reverse(); 43 | } 44 | return; 45 | }; 46 | ``` 47 | 48 | **complexity** 49 | 50 | consider M for NxN total cells in matrix 51 | - Time complexity: O(M). Two passes required 52 | 53 | - Space complexity: O(1) all operations are in place 54 | -------------------------------------------------------------------------------- /coding/rotate_matrix/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_rotate_matrix 3 | name: "Rotate Matrix" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/rotate_matrix/starter.c: -------------------------------------------------------------------------------- 1 | void rotateMatrix(int** matrix, int matrixSize, int* matrixColSize){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/rotate_matrix/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void rotateMatrix(vector>& matrix) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/rotate_matrix/starter.go: -------------------------------------------------------------------------------- 1 | func rotateMatrix(matrix [][]int) { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/rotate_matrix/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void rotateMatrix(int[][] matrix) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/rotate_matrix/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} matrix 3 | * @return {void} Do not return anything, modify matrix in-place instead. 4 | */ 5 | 6 | const rotateMatrix = matrix => { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /coding/rotate_matrix/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - matrix: List[List[int]] 4 | # @return: None 5 | def rotateMatrix(self, matrix): 6 | """ 7 | Do not return anything, modify matrix in-place instead. 8 | """ 9 | pass 10 | -------------------------------------------------------------------------------- /coding/rotate_matrix/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[][]} matrix 2 | # @return {Void} Do not return anything, modify matrix in-place instead. 3 | def rotateMatrix(matrix) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/search_in_rotated_sorted_array/README_en.md: -------------------------------------------------------------------------------- 1 | ## Search in a Rotated Sorted Array 2 | 3 | An array is sorted in ascending order is rotated some number of times. 4 | 5 | Given the array and a target, return the index of target if it is in the array, or -1 if it is not in the array 6 | 7 | You must write an algorithm with O(log n) runtime complexity. 8 | 9 | ### Examples 10 | 11 | Example 1 12 | ``` 13 | Input: nums = [4,5,6,7,0,1,2], target = 0 14 | Output: 4 15 | ``` 16 | 17 | Example 2 18 | ``` 19 | Input: nums = [4,5,6,7,0,1,2], target = 3 20 | Output: -1 21 | ``` 22 | 23 | Example 3: 24 | 25 | ``` 26 | Input: nums = [1], target = 0 27 | Output: -1 28 | ``` 29 | -------------------------------------------------------------------------------- /coding/search_in_rotated_sorted_array/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_search_in_rotated_sorted_array 3 | name: "Search in Rotated Sorted Array" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/search_in_rotated_sorted_array/starter.c: -------------------------------------------------------------------------------- 1 | int searchFor(int* nums, int numsSize, int target){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/search_in_rotated_sorted_array/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int searchFor(vector& nums, int target) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/search_in_rotated_sorted_array/starter.go: -------------------------------------------------------------------------------- 1 | func searchFor(nums []int, target int) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/search_in_rotated_sorted_array/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int searchFor(int[] nums, int target) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/search_in_rotated_sorted_array/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} target 4 | * @return {number} 5 | */ 6 | 7 | const searchFor = (nums, target) => { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /coding/search_in_rotated_sorted_array/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - nums: List[int] 4 | # - target: int 5 | # @return int 6 | def search_for(self, nums, target): 7 | pass 8 | -------------------------------------------------------------------------------- /coding/search_in_rotated_sorted_array/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @param {Integer} target 3 | # @return {Integer} 4 | def searchFor(nums, target) 5 | 6 | end 7 | -------------------------------------------------------------------------------- /coding/spiral_matrix/README_en.md: -------------------------------------------------------------------------------- 1 | ## Spiral Matrix 2 | 3 | Given an m x n matrix, return all elements in spiral order. See example 4 | 5 | ### Examples 6 | 7 | Example 1 8 | ``` 9 | Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] 10 | Output: [1,2,3,6,9,8,7,4,5] 11 | ``` 12 | 13 | Example 2 14 | ``` 15 | Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] 16 | Output: [1,2,3,4,8,12,11,10,9,5,6,7] 17 | ``` 18 | -------------------------------------------------------------------------------- /coding/spiral_matrix/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_spiral_matrix 3 | name: "Spiral Matrix" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/spiral_matrix/starter.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Note: The returned array must be malloced, assume caller calls free(). 3 | */ 4 | int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize){ 5 | 6 | } 7 | -------------------------------------------------------------------------------- /coding/spiral_matrix/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector spiralOrder(vector>& matrix) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/spiral_matrix/starter.go: -------------------------------------------------------------------------------- 1 | func spiralOrder(matrix [][]int) []int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/spiral_matrix/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List spiral(int[][] matrix) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/spiral_matrix/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} matrix 3 | * @return {number[]} 4 | */ 5 | 6 | const spiral = (matrix) => { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /coding/spiral_matrix/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - matrix: List[List[int]] 4 | # @return: List[int] 5 | def spiral(self, matrix): 6 | pass 7 | -------------------------------------------------------------------------------- /coding/spiral_matrix/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[][]} matrix 2 | # @return {Integer[]} 3 | def spiral(matrix) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/three_sum/README_en.md: -------------------------------------------------------------------------------- 1 | ## Three Sum 2 | 3 | Given an integer array nums, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j`, `i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`. 4 | 5 | Notice that the solution set must not contain duplicate triplets. 6 | 7 | ### Examples 8 | 9 | Example 1 10 | ``` 11 | Input: nums = [-1,0,1,2,-1,-4] 12 | Output: [[-1,-1,2],[-1,0,1]] 13 | ``` 14 | 15 | Example 2 16 | ``` 17 | Input: nums = [0] 18 | Output: [] 19 | ``` 20 | 21 | ### **Constraints:** 22 | 23 | - `0 <= nums.length <= 3000` 24 | - `-105 <= nums[i] <= 105` 25 | -------------------------------------------------------------------------------- /coding/three_sum/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for Three Sum 2 | 3 | ### Approach 1: Two Pointers 4 | 5 | **algorithm** 6 | 7 | Keep one number fixed using a for loop, and then use two pointers pattern from Two Sum. To use two pointers pattern, we need the array to be sorted. Sorting the array takes O(n log n) time for n elements in the array. As we are using two nested loops, the overall time complexity remains O(n^2). 8 | 9 | corner cases to consider: 10 | * null/empty input 11 | * duplicates 12 | * this is a problem with a lot of variations such as summing to a different target, not allowing to sort, etc 13 | * if using built in sort, it will be good to know language's built in sort algorithm 14 | 15 | **implementation** 16 | 17 | ```python 18 | class Solution: 19 | def threeSum(self, nums: List[int]) -> List[List[int]]: 20 | if not nums: 21 | return [] 22 | triplets = [] 23 | n = len(nums) 24 | nums.sort() 25 | for i in range(n-2): 26 | if i > 0 and nums[i] == nums[i-1]: continue 27 | j, k = i + 1, n - 1 28 | while j < k: 29 | sum = nums[i] + nums[j] + nums[k] 30 | if sum == 0: 31 | triplets.append([nums[i], nums[j], nums[k]]) 32 | j += 1 33 | k -= 1 34 | while j < k and nums[j] == nums[j-1]: j += 1 35 | elif sum < 0: 36 | j += 1 37 | else: 38 | k -= 1 39 | 40 | return triplets 41 | ``` 42 | 43 | ```javascript 44 | const threeSum = nums => { 45 | if (nums === null || nums.length < 3) return []; 46 | const triplets = [], n = nums.length; 47 | nums.sort((a,b) => a-b); 48 | for (let i = 0; i < n-2; i++){ 49 | if (i > 0 && nums[i] === nums[i-1]) continue; 50 | let j = i + 1, k = n-1; 51 | while (j < k){ 52 | let sum = nums[i] + nums[j] + nums[k]; 53 | if (sum === 0){ 54 | triplets.push([nums[i], nums[j], nums[k]]); 55 | j++; 56 | while (nums[j] === nums[j-1]) j++; 57 | } else if (sum < 0){ 58 | j++; 59 | while (nums[j] === nums[j-1]) j++; 60 | } else { 61 | k--; 62 | while (nums[k] === nums[k+1]) k--; 63 | } 64 | } 65 | } 66 | 67 | return triplets; 68 | } 69 | ``` 70 | 71 | ***complexity*** 72 | 73 | * Time complexity: O(n log n + n^2). This is asymptotically O(n^2) 74 | 75 | * Space complexity: O(log n) for recursive stack space of sorting algorithm. O(1) for pointers 76 | 77 | 78 | -------------------------------------------------------------------------------- /coding/three_sum/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_three_sum 3 | name: "Three Sum" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | industries: 19 | -------------------------------------------------------------------------------- /coding/three_sum/starter.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | /** 4 | * Return an array of arrays of size *returnSize. 5 | * The sizes of the arrays are returned as *returnColumnSizes array. 6 | * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free(). 7 | */ 8 | int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){ 9 | 10 | } 11 | -------------------------------------------------------------------------------- /coding/three_sum/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> threeSum(vector& nums) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/three_sum/starter.go: -------------------------------------------------------------------------------- 1 | func threeSum(nums []int) [][]int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/three_sum/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> threeSum(int[] nums) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/three_sum/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number[]} 4 | */ 5 | 6 | const threeSum = nums => { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /coding/three_sum/starter.py: -------------------------------------------------------------------------------- 1 | 2 | class Solution: 3 | # @params: 4 | # - nums: List[int] 5 | # @return: List[List[int]] 6 | def threeSum(self, nums): 7 | pass 8 | -------------------------------------------------------------------------------- /coding/three_sum/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @return {Integer[][]} 3 | def three_sum(nums) 4 | 5 | end 6 | -------------------------------------------------------------------------------- /coding/top_k_frequent_elements/README_en.md: -------------------------------------------------------------------------------- 1 | ## Top K Frequent Elements 2 | 3 | Given an integer array and an integer k, return the k most freqent elements in any order 4 | 5 | ### Examples 6 | 7 | Example 1 8 | 9 | ``` 10 | Input: nums = [1,1,1,2,2,3], k = 2 11 | Output: [1,2] 12 | ``` 13 | 14 | Example 2 15 | ``` 16 | Input: nums = [1], k = 1 17 | Output: [1] 18 | ``` 19 | 20 | ### **Constraints:** 21 | 22 | - `1 <= nums.length <= 105` 23 | - `k` is in the range `[1, the number of unique elements in the array]`. 24 | - It is **guaranteed** that the answer is **unique**. 25 | 26 | ### **Follow up:** Your algorithm's time complexity must be better than `O(n log n)`, where n is the array's size. 27 | -------------------------------------------------------------------------------- /coding/top_k_frequent_elements/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_top_k_frequent_elements 3 | name: "Top K Frequent Elements" 4 | category: coding 5 | subcategory: "heap" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | industries: 19 | -------------------------------------------------------------------------------- /coding/top_k_frequent_elements/starter.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | /** 4 | * Note: The returned array must be malloced, assume caller calls free(). 5 | */ 6 | int* topKFrequent(int* nums, int numsSize, int k, int* returnSize){ 7 | 8 | } 9 | -------------------------------------------------------------------------------- /coding/top_k_frequent_elements/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector topKFrequent(vector& nums, int k) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/top_k_frequent_elements/starter.go: -------------------------------------------------------------------------------- 1 | func topKFrequent(nums []int, k int) []int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/top_k_frequent_elements/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] topKFrequent(int[] nums, int k) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/top_k_frequent_elements/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} k 4 | * @return {number[]} 5 | */ 6 | 7 | const topKFrequent = (nums, k) => { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /coding/top_k_frequent_elements/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - nums: List[int] 4 | # - k: int 5 | # @return: List[int] 6 | def topKFrequent(self, nums, k): 7 | pass 8 | -------------------------------------------------------------------------------- /coding/top_k_frequent_elements/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @param {Integer} k 3 | # @return {Integer[]} 4 | def top_k_frequent(nums, k) 5 | 6 | end 7 | -------------------------------------------------------------------------------- /coding/two_sum/README_en.md: -------------------------------------------------------------------------------- 1 | ## Two Sum 2 | 3 | Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. 4 | 5 | You may assume that each input would have **exactly one solution**, and you may not use the same element twice. 6 | 7 | You can return the answer in any order. 8 | 9 | 10 | ### Examples 11 | 12 | Example 1 13 | ``` 14 | Input: nums = [2,7,11,15], target = 9 15 | Output: [0,1] 16 | Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. 17 | ``` 18 | 19 | Example 2 20 | ``` 21 | Input: nums = [3,2,4], target = 6 22 | Output: [1,2] 23 | ``` 24 | 25 | Example 3 26 | ``` 27 | Input: nums = [3,3], target = 6 28 | Output: [0,1] 29 | ``` 30 | 31 | ### Constraints: 32 | 33 | * 2 <= nums.length <= 104 34 | * -109 <= nums[i] <= 109 35 | * -109 <= target <= 109 36 | * Only one valid answer exists. 37 | 38 | Follow-up: Can you come up with an algorithm that is less than O(n^2) time complexity? 39 | -------------------------------------------------------------------------------- /coding/two_sum/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_two_sum 3 | name: "Two Sum" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: easy 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | - education 21 | -------------------------------------------------------------------------------- /coding/two_sum/starter.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Note: The returned array must be malloced, assume caller calls free(). 3 | */ 4 | int* twoSum(int* nums, int numsSize, int target, int* returnSize){ 5 | 6 | } 7 | -------------------------------------------------------------------------------- /coding/two_sum/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& nums, int target) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/two_sum/starter.go: -------------------------------------------------------------------------------- 1 | func twoSum(nums []int, target int) []int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/two_sum/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/two_sum/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} target 4 | * @return {number[]} 5 | */ 6 | var twoSum = function(nums, target) { 7 | 8 | }; 9 | -------------------------------------------------------------------------------- /coding/two_sum/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - nums: List[int] 4 | # - target: int 5 | # @return: List[int] 6 | def twoSum(self, nums, target): 7 | pass 8 | -------------------------------------------------------------------------------- /coding/two_sum/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @param {Integer} target 3 | # @return {Integer[]} 4 | def two_sum(nums, target) 5 | 6 | end 7 | -------------------------------------------------------------------------------- /coding/unique_paths/README_en.md: -------------------------------------------------------------------------------- 1 | ## Unique Paths 2 | 3 | Starting with m x n grid. A robot starting at grid[0][0] needs to move to bottom right corner at grid[m - 1][n - 1] 4 | The robot can only move one cell at a time either down or right. 5 | 6 | Given m and n, Return the number of distinct paths the robot can take. 7 | 8 | 9 | ### Examples 10 | 11 | Example 1 12 | ``` 13 | Input: m = 3, n = 7 14 | Output: 28 15 | ``` 16 | 17 | Example 2 18 | ``` 19 | Input: m = 3, n = 2 20 | Output: 3 21 | Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner: 22 | 1. Right -> Down -> Down 23 | 2. Down -> Down -> Right 24 | 3. Down -> Right -> Down 25 | `````` 26 | -------------------------------------------------------------------------------- /coding/unique_paths/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | ## Solution for Unique Paths 2 | 3 | Start with the recursive nature of the problem. The robot can only go down or right. If m or n is 1, there is only one path to take. For 2x2 grid, total paths are the sum of paths going down first + sum of paths going to the right first 4 | 5 | ```python 6 | class Solution: 7 | def distinctPaths(self, m: int, n: int) -> int: 8 | if m == 1 or n == 1: 9 | return 1 10 | return self.uniquePaths(m-1,n) + self.uniquePaths(m, n-1) 11 | ``` 12 | 13 | For large grids, the recursive stack space can get fairly deep. 14 | 15 | ### Approach 1: Dynamic Programming 16 | 17 | **algorithm** 18 | 19 | Use a 2D table to tabulate the number of ways to reach each cell. For any m x n grid, there's only 1 way to reach any cell along the top row or the left most column. We can initiate every cell in the table with a 1. 20 | - iterate starting at grid[1][1] 21 | - every cell's count of paths will be the sum of the number of paths reaching the cell directly above + the number paths reaching the cell directly to the left. ways[row][col] = ways[row-1][col] + ways[row][col-1] 22 | - after iterating through grid, our 2D table will have the sum of possible paths reaching the bottom right corner and we can return it. 23 | 24 | **implementation** 25 | 26 | ```python 27 | class Solution: 28 | def distinctPaths(self, m: int, n: int) -> int: 29 | ways = [[1] * n for _ in range(m)] 30 | 31 | for row in range(1,m): 32 | for col in range(1,n): 33 | ways[row][col] = ways[row-1][col] + ways[row][col-1] 34 | return ways[m-1][n-1] 35 | ``` 36 | 37 | ```java 38 | class Solution { 39 | public int uniquePaths(int m, int n) { 40 | int[][] dp = new int[m][n]; 41 | dp[0][0] = 1; 42 | for (int row = 1; row < m; row++){ 43 | dp[row][0] = 1; 44 | } 45 | for (int col = 1; col < n; col++){ 46 | dp[0][col] = 1; 47 | } 48 | 49 | for (int row = 1; row < m; row++){ 50 | for (int col = 1; col < n; col++){ 51 | dp[row][col] = dp[row-1][col] + dp[row][col-1]; 52 | } 53 | } 54 | return dp[m-1][n-1]; 55 | } 56 | } 57 | ``` 58 | 59 | ```javascript 60 | const distinctPaths = (m,n) => { 61 | const waysDP = new Array(m).fill().map(row => new Array(n).fill(0)); 62 | waysDP[0][0] = 1; 63 | 64 | for (let i = 0; i < m; i++){ 65 | for (let j = 0; j < n; j++){ 66 | if (i > 0) waysDP[i][j] += waysDP[i-1][j]; 67 | if (j > 0) waysDP[i][j] += waysDP[i][j-1]; 68 | } 69 | } 70 | return waysDP[m-1][n-1]; 71 | } 72 | ``` 73 | 74 | **complexity** 75 | 76 | * Time complexity: O(M x N) one pass through grid 77 | 78 | * Space complexity: O(M x N) tabulation table space 79 | -------------------------------------------------------------------------------- /coding/unique_paths/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_unique_paths 3 | name: "Unique Paths" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/unique_paths/starter.c: -------------------------------------------------------------------------------- 1 | int distinctPaths(int m, int n){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/unique_paths/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int distinctPaths(int m, int n) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/unique_paths/starter.go: -------------------------------------------------------------------------------- 1 | func distinctPaths(m int, n int) int { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/unique_paths/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int distinctPaths(int m, int n) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/unique_paths/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} m 3 | * @param {number} n 4 | * @return {number} 5 | */ 6 | 7 | const distinctPaths = (m,n) => { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /coding/unique_paths/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - m: int 4 | # - n: int 5 | # @return: int 6 | def distinctPaths(self, m, n): 7 | pass 8 | -------------------------------------------------------------------------------- /coding/unique_paths/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer} m 2 | # @param {Integer} n 3 | # @return {Integer} 4 | def distinct_paths(m, n) 5 | 6 | end 7 | -------------------------------------------------------------------------------- /coding/valid_anagram/README_en.md: -------------------------------------------------------------------------------- 1 | ## Valid Anagram 2 | 3 | Given two strings, return true if they are anagrams of each other. Return false if they are not anagrams. 4 | 5 | An anagram is a word, phrase, or name formed by rearranging the letters of another, such as cinema, formed from iceman. 6 | 7 | ### Examples 8 | 9 | Example 1 10 | ``` 11 | Input: s = "cinema", t = "iceman" 12 | Output: true 13 | ``` 14 | 15 | Example 2 16 | ``` 17 | Input: s = "house", t = "mouse" 18 | Output: false 19 | ``` 20 | -------------------------------------------------------------------------------- /coding/valid_anagram/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_valid_anagram 3 | name: "Valid Anagram" 4 | category: coding 5 | subcategory: "hash table" 6 | difficulty: easy 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/valid_anagram/starter.c: -------------------------------------------------------------------------------- 1 | bool isValidAnagram(char * s, char * t){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/valid_anagram/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isValidAnagram(string s, string t) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/valid_anagram/starter.go: -------------------------------------------------------------------------------- 1 | func isValidAnagram(s string, t string) bool { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/valid_anagram/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isValidAnagram(String s, String t) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/valid_anagram/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string} s 3 | * @param {string} t 4 | * @return {boolean} 5 | */ 6 | const isValidAnagram = (s, t) => { 7 | 8 | } 9 | -------------------------------------------------------------------------------- /coding/valid_anagram/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - s: str 4 | # - t: str 5 | # @return: bool 6 | def isValidAnagram(self, s, t): 7 | pass 8 | -------------------------------------------------------------------------------- /coding/valid_anagram/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {String} s 2 | # @param {String} t 3 | # @return {Boolean} 4 | def is_valid_anagram(s, t) 5 | 6 | end 7 | -------------------------------------------------------------------------------- /coding/word_search/README_en.md: -------------------------------------------------------------------------------- 1 | ## Word Search 2 | 3 | Given m x n grid of characters and a string, return true if the string exists in the grid of characters. 4 | 5 | Words are made by searching neighboring cells either horizontally or vertically. The same cell cannot be used more than once during a search. 6 | 7 | ### Examples 8 | 9 | Example 1 10 | ``` 11 | Input: board = [ 12 | ["A","B","C","E"], 13 | ["S","F","C","S"], 14 | ["A","D","E","E"] 15 | ], word = "SEE" 16 | Output: true 17 | ``` 18 | 19 | Example 2 20 | ``` 21 | Input: board = [ 22 | ["A","B","C","E"], 23 | ["S","F","C","S"], 24 | ["A","D","E","E"] 25 | ], word = "ABCB" 26 | Output: false 27 | ``` 28 | -------------------------------------------------------------------------------- /coding/word_search/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: coding_word_search 3 | name: "Word Search" 4 | category: coding 5 | subcategory: "array" 6 | difficulty: medium 7 | description: 8 | en: README_en.md 9 | solution: 10 | en: SOLUTION_en.md 11 | starter_code: 12 | c: "starter.c" 13 | cpp: "starter.cpp" 14 | go: "starter.go" 15 | javascript: "starter.js" 16 | python: "starter.py" 17 | ruby: "starter.rb" 18 | java: "starter.java" 19 | industries: 20 | -------------------------------------------------------------------------------- /coding/word_search/starter.c: -------------------------------------------------------------------------------- 1 | bool wordSearch(char** board, int boardSize, int* boardColSize, char * word){ 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/word_search/starter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool wordSearch(vector>& board, string word) { 4 | 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /coding/word_search/starter.go: -------------------------------------------------------------------------------- 1 | func wordSearch(board [][]byte, word string) bool { 2 | 3 | } 4 | -------------------------------------------------------------------------------- /coding/word_search/starter.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean wordSearch(char[][] board, String word) { 3 | 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /coding/word_search/starter.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {character[][]} board 3 | * @param {string} word 4 | * @return {boolean} 5 | */ 6 | 7 | const wordSearch = (board, word) => { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /coding/word_search/starter.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @params: 3 | # - board: List[List[str]] 4 | # - word: str 5 | # @return: bool 6 | def word_search(self, board, word): 7 | pass 8 | -------------------------------------------------------------------------------- /coding/word_search/starter.rb: -------------------------------------------------------------------------------- 1 | # @param {Character[][]} board 2 | # @param {String} word 3 | # @return {Boolean} 4 | def wordSearch(board, word) 5 | 6 | end 7 | -------------------------------------------------------------------------------- /system_design/news_feed/README_en.md: -------------------------------------------------------------------------------- 1 | # Design social media news feed 2 | 3 | Design a social media news feed like Twitter Timeline and Search 4 | 5 | ## Requirements: 6 | 7 | In actual interview, one should always clarify requirements at the beginning of the interview. 8 | In mock interview, the requirements are specified to help the users move forward 9 | 10 | We'll scope the problem to handle only the following use cases 11 | - User posts a tweet 12 | - Service pushes tweets to followers, sending push notifications and emails 13 | - User views the user timeline (activity from the user) 14 | - User views the home timeline (activity from people the user is following) 15 | - User searches keywords 16 | - Service has high availability 17 | 18 | **Out of scope** 19 | 20 | - Service pushes tweets to the Twitter Firehose and other streams 21 | - Service strips out tweets based on users' visibility settings 22 | - Hide @reply if the user is not also following the person being replied to 23 | - Respect 'hide retweets' setting 24 | - Analytics 25 | -------------------------------------------------------------------------------- /system_design/news_feed/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: system_design_news_feed 3 | name: "News Feed" 4 | category: system_design 5 | difficulty: medium 6 | description: 7 | en: README_en.md 8 | solution: 9 | en: SOLUTION_en.md 10 | industries: 11 | - social_media 12 | -------------------------------------------------------------------------------- /system_design/online_chat/README_en.md: -------------------------------------------------------------------------------- 1 | # Design online chat 2 | 3 | ## Constraints and assumptions 4 | Assume we'll focus on the following workflows: 5 | 6 | * Text conversations only 7 | * Users 8 | - Add a user 9 | - Remove a user 10 | - Update a user 11 | - Add to a user's friends list 12 | - Add friend request 13 | - Approve friend request 14 | - Reject friend request 15 | - Remove from a user's friends list 16 | - Create a group chat 17 | - Invite friends to a group chat 18 | - Post a message to a group chat 19 | - Private 1-1 chat 20 | - Invite a friend to a private chat 21 | - Post a meesage to a private chat 22 | 23 | * No need to worry about scaling initially 24 | -------------------------------------------------------------------------------- /system_design/online_chat/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | # Solution for designing online chat 2 | 3 | ## Constraints and assumptions 4 | Assume we'll focus on the following workflows: 5 | 6 | * Text conversations only 7 | * Users 8 | - Add a user 9 | - Remove a user 10 | - Update a user 11 | - Add to a user's friends list 12 | - Add friend request 13 | - Approve friend request 14 | - Reject friend request 15 | - Remove from a user's friends list 16 | - Create a group chat 17 | - Invite friends to a group chat 18 | - Post a message to a group chat 19 | - Private 1-1 chat 20 | - Invite a friend to a private chat 21 | - Post a meesage to a private chat 22 | 23 | * No need to worry about scaling initially 24 | 25 | ## Solution 26 | 27 | ```python 28 | from abc import ABCMeta 29 | 30 | 31 | class UserService(object): 32 | 33 | def __init__(self): 34 | self.users_by_id = {} # key: user id, value: User 35 | 36 | def add_user(self, user_id, name, pass_hash): # ... 37 | def remove_user(self, user_id): # ... 38 | def add_friend_request(self, from_user_id, to_user_id): # ... 39 | def approve_friend_request(self, from_user_id, to_user_id): # ... 40 | def reject_friend_request(self, from_user_id, to_user_id): # ... 41 | 42 | 43 | class User(object): 44 | 45 | def __init__(self, user_id, name, pass_hash): 46 | self.user_id = user_id 47 | self.name = name 48 | self.pass_hash = pass_hash 49 | self.friends_by_id = {} # key: friend id, value: User 50 | self.friend_ids_to_private_chats = {} # key: friend id, value: private chats 51 | self.group_chats_by_id = {} # key: chat id, value: GroupChat 52 | self.received_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest 53 | self.sent_friend_requests_by_friend_id = {} # key: friend id, value: AddRequest 54 | 55 | def message_user(self, friend_id, message): # ... 56 | def message_group(self, group_id, message): # ... 57 | def send_friend_request(self, friend_id): # ... 58 | def receive_friend_request(self, friend_id): # ... 59 | def approve_friend_request(self, friend_id): # ... 60 | def reject_friend_request(self, friend_id): # ... 61 | 62 | 63 | class Chat(metaclass=ABCMeta): 64 | 65 | def __init__(self, chat_id): 66 | self.chat_id = chat_id 67 | self.users = [] 68 | self.messages = [] 69 | 70 | 71 | class PrivateChat(Chat): 72 | 73 | def __init__(self, first_user, second_user): 74 | super(PrivateChat, self).__init__() 75 | self.users.append(first_user) 76 | self.users.append(second_user) 77 | 78 | 79 | class GroupChat(Chat): 80 | 81 | def add_user(self, user): # ... 82 | def remove_user(self, user): # ... 83 | 84 | 85 | class Message(object): 86 | 87 | def __init__(self, message_id, message, timestamp): 88 | self.message_id = message_id 89 | self.message = message 90 | self.timestamp = timestamp 91 | 92 | 93 | class AddRequest(object): 94 | 95 | def __init__(self, from_user_id, to_user_id, request_status, timestamp): 96 | self.from_user_id = from_user_id 97 | self.to_user_id = to_user_id 98 | self.request_status = request_status 99 | self.timestamp = timestamp 100 | 101 | 102 | class RequestStatus(Enum): 103 | 104 | UNREAD = 0 105 | READ = 1 106 | ACCEPTED = 2 107 | REJECTED = 3 108 | ``` 109 | -------------------------------------------------------------------------------- /system_design/online_chat/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: system_design_online_chat 3 | name: "Online Chat" 4 | category: system_design 5 | difficulty: medium 6 | description: 7 | en: README_en.md 8 | solution: 9 | en: SOLUTION_en.md 10 | industries: 11 | - social_media 12 | -------------------------------------------------------------------------------- /system_design/parking_lot/README_en.md: -------------------------------------------------------------------------------- 1 | # Design a parking lot 2 | 3 | ## Constraints and assumptions 4 | 5 | **What types of vehicles should we support?** 6 | Motorcycle, Car, Bus 7 | 8 | **Does each vehicle type take up a different amount of parking spots?** 9 | Yes 10 | ``` 11 | Motorcycle spot -> Motorcycle 12 | Compact spot -> Motorcycle, Car 13 | Large spot -> Motorcycle, Car 14 | Bus can park if we have 5 consecutive "large" spots 15 | ``` 16 | 17 | **Does the parking lot have multiple levels?** 18 | Yes 19 | -------------------------------------------------------------------------------- /system_design/parking_lot/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: system_design_parking_lot 3 | name: "Parking Lot" 4 | category: system_design 5 | difficulty: medium 6 | description: 7 | en: README_en.md 8 | solution: 9 | en: SOLUTION_en.md 10 | industries: 11 | -------------------------------------------------------------------------------- /system_design/photo_sharing/README_en.md: -------------------------------------------------------------------------------- 1 | # Design photo sharing app 2 | 3 | Design a photo sharing app similar to Pinterest 4 | 5 | ## Requirements: 6 | 7 | Pinterest is a highly scalable photo-sharing service: 8 | 9 | - features: user profile, upload photos, news feed, etc. 10 | - scaling out: horizontal scalability and micro services. 11 | -------------------------------------------------------------------------------- /system_design/photo_sharing/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: system_design_photo_sharing 3 | name: "Photo Sharing" 4 | category: system_design 5 | difficulty: medium 6 | description: 7 | en: README_en.md 8 | solution: 9 | en: SOLUTION_en.md 10 | industries: 11 | - finance 12 | - education 13 | - ecommerce 14 | - social_media 15 | - dev_tools 16 | -------------------------------------------------------------------------------- /system_design/ride-share/README_en.md: -------------------------------------------------------------------------------- 1 | # Design ride share app 2 | 3 | Design a ride sharing application similar to Uber and Lyft. 4 | 5 | ## Requirements: 6 | 7 | - ride hailing service targeting the transportation markets around the world 8 | - realtime dispatch in massive scale 9 | - focus on backend design 10 | -------------------------------------------------------------------------------- /system_design/ride-share/SOLUTION_en.md: -------------------------------------------------------------------------------- 1 | # Solution for designing a ride sharing app 2 | 3 | ## Requirements 4 | 5 | * ride hailing service targeting the transportation markets around the world 6 | * realtime dispatch in massive scale 7 | * backend design 8 | 9 | ## Architecture 10 | 11 | ![uber architecture](https://res.cloudinary.com/dohtidfqh/image/upload/v1546574738/web-guiguio/uber-architecture_2.jpg) 12 | 13 | 14 | ## Why micro services? 15 | ==Conway's law== says structures of software systems are copies of the organization structures. 16 | 17 | | | Monolithic ==Service== | Micro Services | 18 | |--- |--- |--- | 19 | | Productivity, when teams and codebases are small | ✅ High | ❌ Low | 20 | | ==Productivity, when teams and codebases are large== | ❌ Low | ✅ High (Conway's law) | 21 | | ==Requirements on Engineering Quality== | ❌ High (under-qualified devs break down the system easily) | ✅ Low (runtimes are segregated) | 22 | | Dependency Bump | ✅ Fast (centrally managed) | ❌ Slow | 23 | | Multi-tenancy support / Production-staging Segregation | ✅ Easy | ❌ Hard (each individual service has to either 1) build staging env connected to others in staging 2) Multi-tenancy support across the request contexts and data storage) | 24 | | Debuggability, assuming same modules, metrics, logs | ❌ Low | ✅ High (w/ distributed tracing) | 25 | | Latency | ✅ Low (local) | ❌ High (remote) | 26 | | DevOps Costs | ✅ Low (High on building tools) | ❌ High (capacity planning is hard) | 27 | 28 | Combining monolithic ==codebase== and micro services can bring benefits from both sides. 29 | 30 | ## Dispatch Service 31 | 32 | * consistent hashing sharded by geohash 33 | * data is transient, in memory, and thus there is no need to replicate. (CAP: AP over CP) 34 | * single-threaded or locked matching in a shard to prevent double dispatching 35 | 36 | 37 | 38 | ## Payment Service 39 | 40 | ==The key is to have an async design==, because payment systems usually have a very long latency for ACID transactions across multiple systems. 41 | 42 | * leverage event queues 43 | * payment gateway w/ Braintree, PayPal, Card.io, Alipay, etc. 44 | * logging intensively to track everything 45 | * [APIs with idempotency, exponential backoff, and random jitter](https://puncsky.com/notes/43-how-to-design-robust-and-predictable-apis-with-idempotency) 46 | 47 | 48 | ## UserProfile Service and Trip Service 49 | 50 | * low latency with caching 51 | * UserProfile Service has the challenge to serve users in increasing types (driver, rider, restaurant owner, eater, etc) and user schemas in different regions and countries. 52 | 53 | ## Push Notification Service 54 | 55 | * Apple Push Notifications Service (not quite reliable) 56 | * Google Cloud Messaging Service GCM (it can detect the deliverability) or 57 | * SMS service is usually more reliable 58 | -------------------------------------------------------------------------------- /system_design/ride-share/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: system_design_ride_share 3 | name: "Ride Share" 4 | category: system_design 5 | difficulty: medium 6 | description: 7 | en: README_en.md 8 | solution: 9 | en: SOLUTION_en.md 10 | industries: 11 | - transporation 12 | -------------------------------------------------------------------------------- /system_design/spending_tracker/README_en.md: -------------------------------------------------------------------------------- 1 | # Design spending tracker 2 | 3 | Design an app similar to Mint.com 4 | 5 | #### We'll scope the problem to handle only the following use cases 6 | 7 | * **User** connects to a financial account 8 | * **Service** extracts transactions from the account 9 | * Updates daily 10 | * Categorizes transactions 11 | * Allows manual category override by the user 12 | * No automatic re-categorization 13 | * Analyzes monthly spending, by category 14 | * **Service** recommends a budget 15 | * Allows users to manually set a budget 16 | * Sends notifications when approaching or exceeding budget 17 | * **Service** has high availability 18 | 19 | #### Out of scope 20 | 21 | * **Service** performs additional logging and analytics 22 | -------------------------------------------------------------------------------- /system_design/spending_tracker/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: system_design_spending_tracker 3 | name: "Spending Tracker" 4 | category: system_design 5 | difficulty: medium 6 | description: 7 | en: README_en.md 8 | solution: 9 | en: SOLUTION_en.md 10 | industries: 11 | - finance 12 | -------------------------------------------------------------------------------- /system_design/url_shortening/README_en.md: -------------------------------------------------------------------------------- 1 | # Design a URL shortening service 2 | 3 | Let's design a URL shortening service like bit.ly. This service will provide short aliases redirecting to long URLs. 4 | 5 | ULR shortening is used to create shorter and sometimes more memorable aliases for long URLs. Users are redirected to the original URL when they hit short urls. 6 | 7 | ## Requirements: 8 | 9 | Our URL shortening system should meet the following requirements: 10 | 11 | **Functional Requirements**: 12 | 13 | 1. Given a URL, our service should generate a shorter and unique URL. 14 | 2. When users access a shortened URL, our service should redirect it to the original long URL. 15 | 3. A premium user should be able to pick a custom alias for their URL. 16 | 4. A shortened URL will expire after a standard default timespan. A premium user should be able to specify the expiration time. 17 | 18 | **Non-Functional Requirements**: 19 | 20 | 1. Our service should be available at all times. 21 | 2. The URL redirect should happen as fast as possible. 22 | 3. Our service can be used by millions of users. 23 | -------------------------------------------------------------------------------- /system_design/url_shortening/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: system_design_url_shortening 3 | name: "URL shortening" 4 | category: system_design 5 | difficulty: medium 6 | description: 7 | en: README_en.md 8 | solution: 9 | en: SOLUTION_en.md 10 | industries: 11 | - social_media 12 | - dev_tools 13 | -------------------------------------------------------------------------------- /system_design/web_crawler/README_en.md: -------------------------------------------------------------------------------- 1 | # Design a web crawler 2 | 3 | #### We'll scope the problem to handle only the following use cases 4 | 5 | * **Service** crawls a list of urls: 6 | * Generates reverse index of words to pages containing the search terms 7 | * Generates titles and snippets for pages 8 | * Title and snippets are static, they do not change based on search query 9 | * **User** inputs a search term and sees a list of relevant pages with titles and snippets the crawler generated 10 | * Only sketch high level components and interactions for this use case, no need to go into depth 11 | * **Service** has high availability 12 | 13 | #### Out of scope 14 | 15 | * Search analytics 16 | * Personalized search results 17 | * Page rank 18 | -------------------------------------------------------------------------------- /system_design/web_crawler/meta.yml: -------------------------------------------------------------------------------- 1 | --- 2 | identifier: system_design_web_crawler 3 | name: "Web Crawler" 4 | category: system_design 5 | difficulty: medium 6 | description: 7 | en: README_en.md 8 | solution: 9 | en: SOLUTION_en.md 10 | industries: 11 | - ecommerce 12 | - dev_tools 13 | --------------------------------------------------------------------------------