Skip to content

Commit

Permalink
Merge pull request #472 from Uvesh99/quiz
Browse files Browse the repository at this point in the history
Added Quiz for DFS BFS Graph Travers
  • Loading branch information
sakeel-103 authored Oct 30, 2024
2 parents f6965e1 + d135935 commit da90b73
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 13 deletions.
17 changes: 16 additions & 1 deletion src/app/components/quiz/quiz.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
margin-top: 100px; /* Move the quiz container down */

margin-top: 8rem;
margin-bottom: 3rem;
padding: 2rem;
font-family: "Roboto", sans-serif;
background-color: #13eea9;
background-color: #f1f1f1;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
Expand Down Expand Up @@ -79,6 +80,7 @@ header {
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); /* Subtle shadow for cards */
cursor: pointer; /* Pointer cursor on hover */
transition: transform 0.2s; /* Transition effect for hover */
margin-right: 10px;
}

.difficulty-card:hover {
Expand Down Expand Up @@ -139,3 +141,16 @@ button:disabled {
background-color: #007bff; /* Color of the score bar */
border-radius: 5px; /* Rounded corners for the score bar */
}
.correct {
color: green;
}

.incorrect {
color: red;
}

.review-card {
border: 1px solid #ddd;
padding: 10px;
margin: 10px 0;
}
16 changes: 14 additions & 2 deletions src/app/components/quiz/quiz.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,22 @@ <h2>Question {{ currentQuestionIndex + 1 }} of {{ questions.length }}</h2>
<h2>Quiz Completed!</h2>
<p>Your score: {{ score }} out of {{ questions.length }}</p>
<div class="score-breakdown">
<div class="score-bar" [style.width.%]="(score / questions.length) * 100"></div>
<div class="score-bar" [style.width.%]="(score / questions.length) * 100"></div>
</div>

<h3>Review Your Answers:</h3>
<div class="review-container">
<div *ngFor="let question of questions; let i = index" class="review-card">
<p><strong>Question {{ i + 1 }}:</strong> {{ question.text }}</p>
<p><strong>Your Answer:</strong> {{ question.options[selectedAnswers[i]] || 'Not Answered' }}</p>
<p [class.correct]="correctAnswers[i]" [class.incorrect]="!correctAnswers[i]">
<strong>Correct Answer:</strong> {{ question.options[question.correctAnswer] }}
</p>
</div>
</div>

<button (click)="restartQuiz()">Retake Quiz</button>
</div>
</div>

<footer>
<p>Created by Graph Theory Enthusiasts</p>
Expand Down
51 changes: 41 additions & 10 deletions src/app/components/quiz/quiz.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ interface Question {
})
export class QuizComponent implements OnInit {


selectedAnswers: number[] = [];
correctAnswers: boolean[] = [];
constructor(private authService: AuthService,private titleService: Title) {}

ngOnInit(): void {
Expand All @@ -52,6 +53,26 @@ export class QuizComponent implements OnInit {
{ text: "What is a complete graph?", options: ["A graph with no edges", "A graph where every pair of vertices is connected", "A graph with a single cycle", "A disconnected graph"], correctAnswer: 1, difficulty: "easy" },
{ text: "Which algorithm can be used to check if a graph is connected?", options: ["Dijkstra's", "DFS", "BFS", "Prim's"], correctAnswer: 1, difficulty: "easy" },
{ text: "How many edges does a tree with n vertices have?", options: ["n-1", "n", "n+1", "2n"], correctAnswer: 0, difficulty: "easy" },
{ text: "Which of the following data structures is commonly used in implementing DFS?", options: ["Queue", "Stack", "Heap", "HashMap"], correctAnswer: 1, difficulty: "medium" },
{ text: "What is the space complexity of DFS when implemented recursively?", options: ["O(V)", "O(E)", "O(V + E)", "O(1)"], correctAnswer: 0, difficulty: "medium" },
{ text: "Which algorithm is used to detect cycles in an undirected graph?", options: ["Dijkstra's", "BFS", "DFS", "Bellman-Ford"], correctAnswer: 2, difficulty: "medium" },
{ text: "In BFS, what is the main purpose of the queue?", options: ["To track visited nodes", "To store path costs", "To keep track of the next level of nodes", "To store parent nodes"], correctAnswer: 2, difficulty: "medium" },
{ text: "Which of the following is true for DFS on a directed acyclic graph (DAG)?", options: ["There are no back edges", "It always finds a cycle", "It has the highest time complexity", "It can only visit each node once"], correctAnswer: 0, difficulty: "medium" },
{ text: "What type of graph traversal is used in level-order traversal of a binary tree?", options: ["DFS", "BFS", "Dijkstra's", "Prim's"], correctAnswer: 1, difficulty: "medium" },
{ text: "Which traversal algorithm can be used to find all nodes reachable from a given starting node?", options: ["DFS only", "BFS only", "Both DFS and BFS", "Neither DFS nor BFS"], correctAnswer: 2, difficulty: "medium" },
{ text: "What is the minimum number of edges required to make a graph connected?", options: ["V", "V - 1", "E + 1", "V + E"], correctAnswer: 1, difficulty: "medium" },
{ text: "Which statement is true about BFS on an unweighted graph?", options: ["It finds the shortest path in terms of edge count", "It always visits nodes in alphabetical order", "It always results in a spanning tree", "It is faster than DFS"], correctAnswer: 0, difficulty: "medium" },
{ text: "What property is always true for a tree?", options: ["It has a cycle", "It is always complete", "It is connected and acyclic", "It has even nodes"], correctAnswer: 2, difficulty: "medium" },
{ text: "In a strongly connected directed graph, what can DFS be used to identify?", options: ["Cycles", "Connected components", "Topological order", "Strongly connected components"], correctAnswer: 3, difficulty: "difficult" },
{ text: "Which algorithm can detect and handle articulation points in a graph?", options: ["DFS", "BFS", "Dijkstra's", "Bellman-Ford"], correctAnswer: 0, difficulty: "difficult" },
{ text: "What is the primary difference in space complexity between BFS and DFS?", options: ["BFS requires more space in general", "DFS requires more space in general", "Both require the same space", "BFS only requires O(1) space"], correctAnswer: 0, difficulty: "difficult" },
{ text: "In an undirected graph, which traversal is more efficient for finding the shortest path in terms of edges?", options: ["DFS", "BFS", "Dijkstra's", "Prim's"], correctAnswer: 1, difficulty: "difficult" },
{ text: "Which algorithm is best suited to find bridges in an undirected graph?", options: ["DFS", "BFS", "Kruskal’s", "Floyd-Warshall"], correctAnswer: 0, difficulty: "difficult" },
{ text: "In a directed graph, what will be the result of performing DFS from a given source node?", options: ["All reachable nodes from the source", "All nodes in the graph", "The shortest path to each node", "The minimum spanning tree"], correctAnswer: 0, difficulty: "difficult" },
{ text: "How many connected components are created when an articulation point is removed from a graph?", options: ["1", "2", "It depends on the graph structure", "None"], correctAnswer: 2, difficulty: "difficult" },
{ text: "Which of the following is true about DFS tree edges in a directed graph?", options: ["They form cycles", "They point to unvisited vertices", "They always connect vertices on the same level", "They are part of the DFS forest"], correctAnswer: 1, difficulty: "difficult" },
{ text: "In BFS, if a graph has cycles, which nodes are revisited?", options: ["No nodes are revisited if marked as visited", "Only nodes in the cycle", "Only nodes with lower indices", "All nodes"], correctAnswer: 0, difficulty: "difficult" },
{ text: "Which of the following techniques can be used to find articulation points in an undirected graph?", options: ["DFS with parent tracking", "BFS with distance matrix", "DFS with stack", "BFS with queue"], correctAnswer: 0, difficulty: "difficult" },
];

currentQuestionIndex: number = 0;
Expand Down Expand Up @@ -95,21 +116,27 @@ export class QuizComponent implements OnInit {
submitAnswer() {
if (this.selectedAnswerIndex === null) return;

this.answerSubmitted = true;
this.animationState = 'flipped';
// Check if the selected answer is correct
const isCorrect = this.selectedAnswerIndex === this.currentQuestion.correctAnswer;
this.correctAnswers.push(isCorrect);
this.selectedAnswers.push(this.selectedAnswerIndex);

if (this.selectedAnswerIndex === this.currentQuestion.correctAnswer) {
// Update score if answer is correct
if (isCorrect) {
this.score++;
}

this.answerSubmitted = true;

// Delay moving to the next question
setTimeout(() => {
this.animationState = 'normal';
if (this.currentQuestionIndex < this.questions.length - 1) {
this.nextQuestion();
} else {
this.answerSubmitted = false;
this.selectedAnswerIndex = null;
this.currentQuestionIndex++;
if (this.currentQuestionIndex >= this.questions.length) {
this.quizCompleted = true;
}
}, 1000);
}, 1000); // Delay to show feedback
}

nextQuestion() {
Expand All @@ -119,11 +146,15 @@ export class QuizComponent implements OnInit {
}

restartQuiz() {
// Reset quiz state
this.quizStarted = false;
this.quizCompleted = false;
this.currentQuestionIndex = 0;
this.selectedAnswerIndex = null;
this.answerSubmitted = false;
this.score = 0;
this.quizCompleted = false;
this.selectedAnswers = [];
this.correctAnswers = [];
this.shuffleQuestions();
}
}

0 comments on commit da90b73

Please sign in to comment.