import React, { useState } from 'react'; import { Code, Play, Check, X, ChevronRight, Trophy, BookOpen, Filter, Search, Clock, Award, CheckCircle } from 'lucide-react'; export default function PracticePlatform() { const [view, setView] = useState('list'); // 'list' or 'solve' const [selectedQuestion, setSelectedQuestion] = useState(null); const [code, setCode] = useState(''); const [language, setLanguage] = useState('javascript'); const [output, setOutput] = useState(''); const [testResults, setTestResults] = useState([]); const [filterDifficulty, setFilterDifficulty] = useState('all'); const [filterTopic, setFilterTopic] = useState('all'); const [searchTerm, setSearchTerm] = useState(''); const questions = [ { id: 1, title: "Two Sum", difficulty: "Easy", topic: "Arrays", solved: true, description: "Given an array of integers nums and an integer target, return indices of the two numbers that add up to target.", examples: [ { input: "[2,7,11,15], target = 9", output: "[0,1]" }, { input: "[3,2,4], target = 6", output: "[1,2]" } ], testCases: [ { input: "[2,7,11,15]\n9", expected: "[0,1]" }, { input: "[3,2,4]\n6", expected: "[1,2]" }, { input: "[3,3]\n6", expected: "[0,1]" } ], starterCode: { javascript: "function twoSum(nums, target) {\n // Your code here\n \n}", python: "def two_sum(nums, target):\n # Your code here\n pass" } }, { id: 2, title: "Reverse String", difficulty: "Easy", topic: "Strings", solved: true, description: "Write a function that reverses a string. The input string is given as an array of characters.", examples: [ { input: '["h","e","l","l","o"]', output: '["o","l","l","e","h"]' }, { input: '["H","a","n","n","a","h"]', output: '["h","a","n","n","a","H"]' } ], testCases: [ { input: '["h","e","l","l","o"]', expected: '["o","l","l","e","h"]' }, { input: '["H","a","n","n","a","h"]', expected: '["h","a","n","n","a","H"]' } ], starterCode: { javascript: "function reverseString(s) {\n // Your code here\n \n}", python: "def reverse_string(s):\n # Your code here\n pass" } }, { id: 3, title: "Valid Parentheses", difficulty: "Medium", topic: "Stack", solved: false, description: "Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.", examples: [ { input: '"()"', output: 'true' }, { input: '"()[]{}"', output: 'true' }, { input: '"(]"', output: 'false' } ], testCases: [ { input: '"()"', expected: 'true' }, { input: '"()[]{}"', expected: 'true' }, { input: '"(]"', expected: 'false' } ], starterCode: { javascript: "function isValid(s) {\n // Your code here\n \n}", python: "def is_valid(s):\n # Your code here\n pass" } }, { id: 4, title: "Binary Search", difficulty: "Easy", topic: "Searching", solved: false, description: "Given a sorted array of integers and a target value, return the index if found. If not, return -1.", examples: [ { input: "nums = [-1,0,3,5,9,12], target = 9", output: "4" }, { input: "nums = [-1,0,3,5,9,12], target = 2", output: "-1" } ], testCases: [ { input: "[-1,0,3,5,9,12]\n9", expected: "4" }, { input: "[-1,0,3,5,9,12]\n2", expected: "-1" } ], starterCode: { javascript: "function search(nums, target) {\n // Your code here\n \n}", python: "def search(nums, target):\n # Your code here\n pass" } }, { id: 5, title: "Merge Two Sorted Lists", difficulty: "Medium", topic: "Linked List", solved: false, description: "Merge two sorted linked lists and return it as a sorted list.", examples: [ { input: "list1 = [1,2,4], list2 = [1,3,4]", output: "[1,1,2,3,4,4]" } ], testCases: [ { input: "[1,2,4]\n[1,3,4]", expected: "[1,1,2,3,4,4]" } ], starterCode: { javascript: "function mergeTwoLists(list1, list2) {\n // Your code here\n \n}", python: "def merge_two_lists(list1, list2):\n # Your code here\n pass" } }, { id: 6, title: "Maximum Subarray", difficulty: "Hard", topic: "Dynamic Programming", solved: false, description: "Given an integer array nums, find the contiguous subarray with the largest sum, and return its sum.", examples: [ { input: "[-2,1,-3,4,-1,2,1,-5,4]", output: "6 (subarray [4,-1,2,1])" } ], testCases: [ { input: "[-2,1,-3,4,-1,2,1,-5,4]", expected: "6" } ], starterCode: { javascript: "function maxSubArray(nums) {\n // Your code here\n \n}", python: "def max_sub_array(nums):\n # Your code here\n pass" } }, { id: 7, title: "Fibonacci Number", difficulty: "Easy", topic: "Recursion", solved: true, description: "Calculate the nth Fibonacci number. The Fibonacci sequence: F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2).", examples: [ { input: "n = 2", output: "1" }, { input: "n = 3", output: "2" }, { input: "n = 4", output: "3" } ], testCases: [ { input: "2", expected: "1" }, { input: "3", expected: "2" }, { input: "4", expected: "3" } ], starterCode: { javascript: "function fib(n) {\n // Your code here\n \n}", python: "def fib(n):\n # Your code here\n pass" } }, { id: 8, title: "Find Peak Element", difficulty: "Medium", topic: "Arrays", solved: false, description: "Find a peak element in an array. A peak element is greater than its neighbors.", examples: [ { input: "[1,2,3,1]", output: "2 (index where 3 is located)" } ], testCases: [ { input: "[1,2,3,1]", expected: "2" } ], starterCode: { javascript: "function findPeakElement(nums) {\n // Your code here\n \n}", python: "def find_peak_element(nums):\n # Your code here\n pass" } } ]; const topics = ['all', 'Arrays', 'Strings', 'Stack', 'Searching', 'Linked List', 'Dynamic Programming', 'Recursion']; const getDifficultyColor = (difficulty) => { switch(difficulty) { case 'Easy': return 'text-green-400 bg-green-400/10 border-green-400/30'; case 'Medium': return 'text-yellow-400 bg-yellow-400/10 border-yellow-400/30'; case 'Hard': return 'text-red-400 bg-red-400/10 border-red-400/30'; default: return 'text-gray-400'; } }; const filteredQuestions = questions.filter(q => { const matchesDifficulty = filterDifficulty === 'all' || q.difficulty === filterDifficulty; const matchesTopic = filterTopic === 'all' || q.topic === filterTopic; const matchesSearch = q.title.toLowerCase().includes(searchTerm.toLowerCase()) || q.topic.toLowerCase().includes(searchTerm.toLowerCase()); return matchesDifficulty && matchesTopic && matchesSearch; }); const openQuestion = (question) => { setSelectedQuestion(question); setCode(question.starterCode[language]); setView('solve'); setOutput(''); setTestResults([]); }; const runCode = () => { try { setOutput('Running test cases...\n'); const results = []; selectedQuestion.testCases.forEach((test, idx) => { try { const logs = []; const originalLog = console.log; console.log = (...args) => logs.push(args.join(' ')); eval(code); const result = logs.join(''); const passed = result.trim() === test.expected.trim(); results.push({ testCase: idx + 1, passed, input: test.input, expected: test.expected, got: result || 'No output' }); console.log = originalLog; } catch (err) { results.push({ testCase: idx + 1, passed: false, error: err.message }); } }); setTestResults(results); const passedCount = results.filter(r => r.passed).length; setOutput(`${passedCount}/${results.length} test cases passed`); } catch (err) { setOutput(`Error: ${err.message}`); } }; const stats = { total: questions.length, solved: questions.filter(q => q.solved).length, easy: questions.filter(q => q.difficulty === 'Easy').length, medium: questions.filter(q => q.difficulty === 'Medium').length, hard: questions.filter(q => q.difficulty === 'Hard').length }; if (view === 'solve' && selectedQuestion) { return (
{/* Header */}

{selectedQuestion.title}

{selectedQuestion.difficulty}
{/* Problem Description */}

{selectedQuestion.title}

Topic: {selectedQuestion.topic}
{selectedQuestion.description}

Examples:

{selectedQuestion.examples.map((ex, idx) => (

Input:

{ex.input}

Output:

{ex.output}

))}
{/* Code Editor & Output */}
{/* Language Selector */}
{/* Code Editor */}