Skip to content

Instantly share code, notes, and snippets.

@shivam-tripathi
Created January 28, 2021 04:13
Show Gist options
  • Save shivam-tripathi/97f3d806bc9fe6dc4147fbe08b2c80ac to your computer and use it in GitHub Desktop.
Save shivam-tripathi/97f3d806bc9fe6dc4147fbe08b2c80ac to your computer and use it in GitHub Desktop.
Get all leetcode problems
const axios = require('axios');
async function main() {
const url = 'https://leetcode.com/api/problems/all/';
const { data } = await axios({ method: 'GET', url });
const getDifficulty = (level) => {
switch(level) {
case 1: return 'easy';
case 2: return 'medium';
case 3: return 'hard';
default: return 'unknown';
}
};
const result = data.stat_status_pairs.sort((a, b) => a.difficulty.level - b.difficulty.level);
const questions = result.reduce((acc, item) => {
const difficultyLevel = getDifficulty(item.difficulty.level);
acc[difficultyLevel] = acc[difficultyLevel] || { problems: [] };
acc[difficultyLevel].problems.push({
url: `https://leetcode.com/problems/${item.stat.question__title_slug}`,
title: item.stat.question__title,
id: item.stat.frontend_question_id,
difficulty: difficultyLevel,
});
return acc;
}, {});
Object.keys(questions).forEach(level => {
questions[level].count = questions[level].problems.length;
});
console.log(JSON.stringify(questions, null, 2));
}
main().catch(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment