Skip to content

Commit e6af625

Browse files
committed
Merge branch 'master' of github.com:lamccloskey/leetcode-patterns
2 parents 3a9fd88 + bde6d92 commit e6af625

File tree

172 files changed

+12471
-4856
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

172 files changed

+12471
-4856
lines changed

.github/workflows/github-pages.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: github-pages
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
workflow_run:
9+
workflows: [run-cron]
10+
types:
11+
- completed
12+
13+
workflow_dispatch:
14+
15+
jobs:
16+
build-and-deploy:
17+
runs-on: ubuntu-20.04
18+
steps:
19+
- name: Checkout Repository
20+
uses: actions/checkout@v3
21+
22+
- name: Install dependencies and build
23+
uses: actions/setup-node@v3
24+
with:
25+
node-version: '14'
26+
- run: npm ci
27+
- run: npm run build
28+
29+
- name: Check GitHub Pages status
30+
uses: crazy-max/ghaction-github-status@v3
31+
with:
32+
pages_threshold: major_outage
33+
34+
- name: Deploy to gh-pages branch
35+
if: success()
36+
uses: crazy-max/ghaction-github-pages@v3
37+
with:
38+
target_branch: gh-pages
39+
build_dir: build
40+
env:
41+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/run-cron.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: run-cron
2+
3+
on:
4+
schedule:
5+
- cron: '0 12 * * 0'
6+
7+
workflow_dispatch:
8+
9+
jobs:
10+
update:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: actions/setup-python@v4
15+
with:
16+
python-version: '3.x'
17+
architecture: 'x64'
18+
- run: |
19+
python -m pip install --upgrade pip
20+
pip install requests python-leetcode
21+
- run: |
22+
python cron/update_questions.py
23+
env:
24+
LEETCODE_SESSION_TOKEN: ${{ secrets.LEETCODE_SESSION_TOKEN }}
25+
- uses: stefanzweifel/git-auto-commit-action@v4
26+
with:
27+
commit_message: Update questions via run-cron GitHub Action

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@
2323
npm-debug.log*
2424
yarn-debug.log*
2525
yarn-error.log*
26+
27+
__pycache__

README.md

Lines changed: 8 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
# Leetcode Patterns
1+
# Leetcode Patterns [![github-pages](https://github.com/seanprashad/leetcode-patterns/actions/workflows/github-pages.yml/badge.svg)](https://github.com/seanprashad/leetcode-patterns/actions/workflows/github-pages.yml) [![run-cron](https://github.com/seanprashad/leetcode-patterns/actions/workflows/run-cron.yml/badge.svg)](https://github.com/seanprashad/leetcode-patterns/actions/workflows/run-cron.yml)
22

33
## Table of Contents
44

55
- [Background](#background)
6-
- [Preface](#preface)
6+
- [Fundamentals](#fundamentals)
77
- [Notes](#notes)
88
- [Question List](#question-list)
99
- [Solutions](#solutions)
10-
- [Leetcode Discuss](#leetcode-discuss)
11-
- [Tips to Consider](#tips-to-consider)
1210
- [Suggestions](#suggestions)
1311
- [Acknowledgements](#acknowledgements)
1412

@@ -22,10 +20,11 @@ repeatedly applying common patterns rather than randomly tackling questions.
2220

2321
All questions are available on [leetcode.com] with some requiring [leetcode premium].
2422

25-
## Preface
23+
## Fundamentals
2624

27-
It is highly recommended to read chapters 1, 2, 3, 4, 8, and 10 of [Cracking The Coding Interview]
28-
to familiarize yourself with the following data structures and their operations:
25+
To find the greatest amount of success when practicing, it is highly recommended
26+
to know the methods and runtimes of the following data structures and their
27+
operations:
2928

3029
- Arrays
3130
- Maps
@@ -45,7 +44,7 @@ In addition, you should have a good grasp on common algorithms such as:
4544

4645
## Notes
4746

48-
[This pdf] contains useful information for the built-in data structures in Java.
47+
[This pdf] contains information for the main data structures in Java.
4948

5049
Other useful methods to know include [`substring()`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#substring-int-int-), [`toCharArray()`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#toCharArray--), [`Math.max()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#max-int-int-),
5150
[`Math.min()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#min-int-int-), and [`Arrays.fill()`](https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#fill-int:A-int-).
@@ -55,62 +54,10 @@ Other useful methods to know include [`substring()`](https://docs.oracle.com/jav
5554
The entire question list can be found here:
5655
https://seanprashad.com/leetcode-patterns/.
5756

58-
In addition to viewing the question list, companies that have previously asked
59-
the question in the past 6 months (_as of May 2021_) will be listed. You can
60-
also use the checkboxes to mark which questions you've completed!
61-
6257
## Solutions
6358

6459
Solutions written in Java can be found in the [solutions] branch.
6560

66-
## Leetcode Discuss
67-
68-
[Leetcode discuss] is an amazing resource and features previous interview
69-
questions, as well as compensation and general career advice.
70-
71-
## Tips to Consider
72-
73-
```
74-
If input array is sorted then
75-
- Binary search
76-
- Two pointers
77-
78-
If asked for all permutations/subsets then
79-
- Backtracking
80-
81-
If given a tree then
82-
- DFS
83-
- BFS
84-
85-
If given a graph then
86-
- DFS
87-
- BFS
88-
89-
If given a linked list then
90-
- Two pointers
91-
92-
If recursion is banned then
93-
- Stack
94-
95-
If must solve in-place then
96-
- Swap corresponding values
97-
- Store one or more different values in the same pointer
98-
99-
If asked for maximum/minimum subarray/subset/options then
100-
- Dynamic programming
101-
102-
If asked for top/least K items then
103-
- Heap
104-
105-
If asked for common strings then
106-
- Map
107-
- Trie
108-
109-
Else
110-
- Map/Set for O(1) time & O(n) space
111-
- Sort input for O(nlogn) time and O(1) space
112-
```
113-
11461
## Suggestions
11562

11663
Think a question should/shouldn't be included? Wish there was another feature?
@@ -119,18 +66,13 @@ Feel free to open an [issue] with your suggestion!
11966
## Acknowledgements
12067

12168
This list is heavily inspired from [Grokking the Coding Interview] with
122-
additional problems extracted from the [Blind 75 list] and this medium article
69+
additional problems extracted from the [Blind 75 list] and this hackernoon article
12370
on [14 patterns to ace any coding interview question].
12471

12572
[leetcode.com]: https://leetcode.com
12673
[leetcode premium]: https://leetcode.com/subscribe/
12774
[this pdf]: https://drive.google.com/open?id=1ao4ZA28zzBttDkuS6MLQI52gDs_CJZEm
128-
[cracking the coding interview]: http://www.crackingthecodinginterview.com/contents.html
129-
[here]: https://hackernoon.com/14-patterns-to-ace-any-coding-interview-question-c5bb3357f6ed
130-
[topcoder]: https://www.topcoder.com/community/competitive-programming/tutorials/dynamic-programming-from-novice-to-advanced/
131-
[back to back swe youtube channel]: https://www.youtube.com/watch?v=jgiZlGzXMBw
13275
[solutions]: https://github.com/SeanPrashad/leetcode-patterns/tree/solutions
133-
[leetcode discuss]: https://leetcode.com/discuss/interview-question
13476
[grokking the coding interview]: https://www.educative.io/courses/grokking-the-coding-interview
13577
[issue]: https://github.com/SeanPrashad/leetcode-patterns/issues/new
13678
[blind 75 list]: https://www.teamblind.com/article/New-Year-Gift---Curated-List-of-Top-100-LeetCode-Questions-to-Save-Your-Time-OaM1orEU?utm_source=share&utm_medium=ios_app

cron/update_questions.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import os
2+
import json
3+
import leetcode
4+
import leetcode.auth
5+
from datetime import datetime
6+
from leetcode.rest import ApiException
7+
8+
9+
def create_leetcode_api():
10+
LEETCODE_SESSION_TOKEN = os.environ.get("LEETCODE_SESSION_TOKEN")
11+
csrf_token = os.environ.get("LEETCODE_CSRF_TOKEN")
12+
13+
configuration = leetcode.Configuration()
14+
15+
configuration.api_key["x-csrftoken"] = csrf_token
16+
configuration.api_key["csrftoken"] = csrf_token
17+
configuration.api_key["LEETCODE_SESSION"] = LEETCODE_SESSION_TOKEN
18+
configuration.api_key["Referer"] = "https://leetcode.com"
19+
configuration.debug = False
20+
21+
return leetcode.DefaultApi(leetcode.ApiClient(configuration))
22+
23+
24+
def get_question_metadata(api, title_slug):
25+
graphql_request = leetcode.GraphqlQuery(
26+
query='''query questionData($titleSlug: String!) {
27+
question(titleSlug: $titleSlug) {
28+
title
29+
difficulty
30+
companyTagStats
31+
isPaidOnly
32+
}
33+
}
34+
''',
35+
variables=leetcode.GraphqlQueryGetQuestionDetailVariables(
36+
title_slug=title_slug)
37+
)
38+
39+
try:
40+
response = api.graphql_post(body=graphql_request)
41+
return response
42+
except ApiException as e:
43+
print(
44+
f'Exception occurred when contacting the Leetcode GraphQL API: ${e}')
45+
exit()
46+
47+
48+
def construct_company_tag_list(company_tags_json, sections):
49+
companies = []
50+
51+
for section in sections:
52+
for company in company_tags_json[section]:
53+
companies.append({
54+
"name": company["name"],
55+
"slug": company["slug"],
56+
"frequency": company["timesEncountered"]
57+
})
58+
59+
return sorted(companies, key=lambda d: d['frequency'], reverse=True)
60+
61+
62+
def update_question_metadata(question, response):
63+
print(f'''🔄 Updating question metadata for {question["title"]}''')
64+
65+
question_title = response.data.question.title
66+
question_difficulty = response.data.question.difficulty
67+
question_company_tags = json.loads(
68+
response.data.question.company_tag_stats)
69+
question_is_premium = response.data.question.is_paid_only
70+
71+
# Retrieve companies who have asked this question for the following two
72+
# company_tag_stat sections:
73+
# 1. 0-6 months
74+
# 2. 6 months to 1 year
75+
companies = construct_company_tag_list(
76+
question_company_tags, ["1", "2"])
77+
78+
question["title"] = question_title
79+
question["difficulty"] = question_difficulty
80+
question["companies"] = companies
81+
question["premium"] = question_is_premium
82+
83+
84+
def read_questions(file_name):
85+
print(f"💾 Loading {file_name}")
86+
87+
try:
88+
with open(file_name, "r") as file:
89+
questions = json.load(file)
90+
print(f"✅ Finished loading {file_name}")
91+
return questions
92+
except Exception as e:
93+
print(
94+
f"❌ Exception occurred when reading {file_name}: {e}")
95+
exit()
96+
97+
98+
def write_questions(file_name, questions):
99+
print(f"💾 Updating {file_name}")
100+
101+
try:
102+
with open(file_name, "w") as file:
103+
questions["updated"] = str(datetime.now().isoformat())
104+
json.dump(questions, file, indent=2)
105+
print(f"✅ Finished updating {file_name}")
106+
except Exception as e:
107+
print(
108+
f"❌ Exception occurred when writing {file_name}: {e}")
109+
exit()
110+
111+
112+
def main(file_name):
113+
api = create_leetcode_api()
114+
questions = read_questions(file_name)
115+
116+
for question in questions["data"]:
117+
title_slug = question["slug"]
118+
119+
response = get_question_metadata(api, title_slug)
120+
121+
update_question_metadata(question, response)
122+
123+
write_questions(file_name, questions)
124+
125+
126+
if __name__ == "__main__":
127+
file_name = os.getcwd() + "/src/data/questions.json"
128+
startTime = datetime.now()
129+
130+
main(file_name)
131+
132+
print(f"⏱️ Data updated in {datetime.now() - startTime} seconds")

0 commit comments

Comments
 (0)