0% found this document useful (0 votes)
48 views10 pages

? Mastering DSA in C - 35 Questions & Answers ?

The document provides a comprehensive overview of data structures and algorithms, including definitions, differences, and time complexities. It covers various concepts such as arrays, linked lists, stacks, queues, trees, and memory allocation, along with C programming examples for implementation. Additionally, it discusses algorithm analysis, hash tables, and recursion.

Uploaded by

Dinosor gon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views10 pages

? Mastering DSA in C - 35 Questions & Answers ?

The document provides a comprehensive overview of data structures and algorithms, including definitions, differences, and time complexities. It covers various concepts such as arrays, linked lists, stacks, queues, trees, and memory allocation, along with C programming examples for implementation. Additionally, it discusses algorithm analysis, hash tables, and recursion.

Uploaded by

Dinosor gon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DSA

1. What is a data structure?


Answer: A data structure is a way of organizing and storing
data in a computer to perform operations efficiently.

2. Explain the difference between an array and a linked


list.
Answer: An array is a static data structure with a fixed
size, while a linked list is a dynamic data structure where
elements are connected through pointers.

3. What is the time complexity of linear search in an array?


Answer: The time complexity of linear search in an array is
O(n), where n is the number of elements in the array.

4. Describe the concept of recursion.


Answer: Recursion is a programming concept where a
function calls itself in order to solve a smaller instance of
the same problem.

5. What is the significance of the 'void' keyword in C


functions?
Answer: The 'void' keyword in a function declaration
indicates that the function does not return any value.

6. Explain the term 'stack' in the context of data structures.


Answer: A stack is a linear data structure that follows the
Last In, First Out (LIFO) principle, where elements are
added and removed from the same end, called the top.

7. Write a C function to swap two variables without using a


temporary variable.
1. void swap(int *a, int *b) {
2. *a = *a + *b;
3. *b = *a - *b;
4. *a = *a - *b;
5. }
6.

8. What is the purpose of the 'malloc' function in C?


Answer: The 'malloc' function is used to dynamically
allocate memory in C.

9. Explain the concept of a binary tree.


Answer: A binary tree is a hierarchical data structure
where each node has at most two children, referred to as
the left child and the right child.

10. What is the time complexity of the quicksort algorithm


in the average case?
Answer: The average case time complexity of quicksort is
O(n log n).

11. Write a C program to find the factorial of a given


number using recursion.
1. #include <stdio.h>
2.
3. int factorial(int n) {
4. if (n == 0 || n == 1)
5. return 1;
6. else
7. return n * factorial(n - 1);
8. }
9.
10. int main() {
11. int num;
12. printf("Enter a number: ");
13. scanf("%d", &num);
14. printf("Factorial of %d is %d\n", num, factorial(num));
15. return 0;
16. }
17.

12. What is the purpose of the 'sizeof' operator in C?


Answer: The 'sizeof' operator in C is used to determine the
size (in bytes) of a data type or variable.

13. Explain the concept of a doubly linked list.


Answer: In a doubly linked list, each node contains a data
element and two pointers - one pointing to the next node
and another pointing to the previous node.

14. What is a hash table?


Answer: A hash table is a data structure that maps keys to
values, where the keys are hashed using a hash function
to determine the index of the corresponding value in the
table.

15. Write a C function to check if a given string is a


palindrome.

1. #include <stdio.h>
2. #include <string.h>
3.
4. int isPalindrome(char str[]) {
5. int length = strlen(str);
6. for (int i = 0; i < length / 2; i++) {
7. if (str[i] != str[length - i - 1])
8. return 0; // Not a palindrome
9. }
10. return 1; // Palindrome
11. }
12.
13. int main() {
14. char input[100];
15. printf("Enter a string: ");
16. scanf("%s", input);
17. if (isPalindrome(input))
18. printf("It's a palindrome.\n");
19. else
20. printf("It's not a palindrome.\n");
21. return 0;
22. }
23.

16. What is the purpose of the 'typedef' keyword in C?


Answer: The 'typedef' keyword in C is used to create
aliases or alternative names for data types.

17. Explain the concept of a priority queue.


Answer: A priority queue is a data structure that stores
elements with associated priorities, and elements with
higher priorities are served before those with lower
priorities.

18. What is the time complexity of the binary search


algorithm?
Answer: The time complexity of the binary search
algorithm is O(log n), where n is the number of elements in
the array.

19. Write a C program to implement a simple linked list.


1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. struct Node {
5. int data;
6. struct Node* next;
7. };
8.
9. void printList(struct Node* head) {
10. while (head != NULL) {
11. printf("%d -> ", head->data);
12. head = head->next;
13. }
14. printf("NULL\n");
15. }
16.
17. int main() {
18. struct Node* head = NULL;
19. struct Node* second = NULL;
20. struct Node* third = NULL;
21.
22. head = (struct Node*)malloc(sizeof(struct Node));
23. second = (struct Node*)malloc(sizeof(struct Node));
24. third = (struct Node*)malloc(sizeof(struct Node));
25.
26. head->data = 1;
27. head->next = second;
28.
29. second->data = 2;
30. second->next = third;
31.
32. third->data = 3;
33. third->next = NULL;
34.
35. printList(head);
36.
37. return 0;
38. }
39.

20. What is the purpose of the 'const' keyword in C?


Answer: The 'const' keyword in C is used to declare
constants, indicating that the value of the variable cannot
be changed after initialization.

21. What is the difference between a stack and a queue?


Answer: A stack follows the Last In, First Out (LIFO)
principle, while a queue follows the First In, First Out
(FIFO) principle.

22. Explain the concept of dynamic memory allocation in


C.
Answer: Dynamic memory allocation in C involves using
functions like malloc, calloc, realloc, and free to allocate
and deallocate memory during program execution.

23. What is the time complexity of the bubble sort


algorithm?
Answer: The time complexity of the bubble sort algorithm
is O(n^2) in the worst-case.

24. Write a C program to find the greatest common divisor


(GCD) of two numbers using Euclid's algorithm.

1. #include <stdio.h>
2.
3. int gcd(int a, int b) {
4. if (b == 0)
5. return a;
6. else
7. return gcd(b, a % b);
8. }
9.
10. int main() {
11. int num1, num2;
12. printf("Enter two numbers: ");
13. scanf("%d %d", &num1, &num2);
14. printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));
15. return 0;
16. }
17.

25. Explain the concept of a hash collision in a hash table.


Answer: A hash collision occurs when two different keys
hash to the same index in a hash table. Collision
resolution techniques like chaining or open addressing are
used to handle collisions.

26. What is a doubly linked list and how is it different from


a singly linked list?
Answer: In a doubly linked list, each node contains a data
element, a pointer to the next node, and a pointer to the
previous node. Unlike a singly linked list, it allows traversal
in both directions.
27. What is the purpose of the 'break' statement in a
switch statement?
Answer: The 'break' statement in a switch statement is
used to exit the switch and prevent fall-through to
subsequent cases.

28. Explain the concept of time complexity in algorithm


analysis.
Answer: Time complexity is a measure of the amount of
time an algorithm takes to complete as a function of the
size of the input. It helps in analyzing the efficiency of
algorithms.

29. Write a C program to reverse a linked list.


1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. struct Node {
5. int data;
6. struct Node* next;
7. };
8.
9. struct Node* reverseList(struct Node* head) {
10. struct Node* prev = NULL;
11. struct Node* current = head;
12. struct Node* next = NULL;
13.
14. while (current != NULL) {
15. next = current->next;
16. current->next = prev;
17. prev = current;
18. current = next;
19. }
20.
21. return prev; // New head of the reversed list
22. }
23.
24. void printList(struct Node* head) {
25. while (head != NULL) {
26. printf("%d -> ", head->data);
27. head = head->next;
28. }
29. printf("NULL\n");
30. }
31.
32. int main() {
33. struct Node* head = NULL;
34. // Assume head is properly initialized and list is not empty
35.
36. printf("Original List: ");
37. printList(head);
38.
39. head = reverseList(head);
40.
41. printf("Reversed List: ");
42. printList(head);
43.
44. return 0;
45. }
46.

30. What is the purpose of the 'static' keyword in C?


Answer: The 'static' keyword in C has multiple uses,
including making a variable or function local to a file,
preserving the value of a variable between function calls,
and defining a constant variable.

31. Explain the concept of a binary search tree.


Answer: A binary search tree (BST) is a binary tree where
the left subtree of a node contains only nodes with values
less than the node, and the right subtree contains only
nodes with values greater than the node.

32. What is the difference between depth-first search


(DFS) and breadth-first search (BFS)?
Answer: DFS explores as far as possible along one
branch before backtracking, while BFS explores neighbor
nodes before moving on to the next level.

33. Write a C program to implement a basic stack using an


array.
1. #include <stdio.h>
2. #define MAX_SIZE 100
3.
4. struct Stack {
5. int arr[MAX_SIZE];
6. int top;
7. };
8.
9. void initialize(struct Stack *stack) {
10. stack->top = -1;
11. }
12.
13. int isEmpty(struct Stack *stack) {
14. return stack->top == -1;
15. }
16.
17. int isFull(struct Stack *stack) {
18. return stack->top == MAX_SIZE - 1;
19. }
20.
21. void push(struct Stack *stack, int item) {
22. if (isFull(stack)) {
23. printf("Stack Overflow\n");
24. return;
25. }
26. stack->arr[++stack->top] = item;
27. }
28.
29. int pop(struct Stack *stack) {
30. if (isEmpty(stack)) {
31. printf("Stack Underflow\n");
32. return -1;
33. }
34. return stack->arr[stack->top--];
35. }
36.
37. int main() {
38. struct Stack stack;
39. initialize(&stack);
40.
41. push(&stack, 10);
42. push(&stack, 20);
43. push(&stack, 30);
44.
45. printf("Popped item: %d\n", pop(&stack));
46.
47. return 0;
48. }
49.

34. What is the purpose of the 'const' keyword in function


declarations?
Answer: In function declarations, the 'const' keyword is
used to indicate that the function does not modify the
values of the parameters.

35. Explain the concept of a circular linked list.


Answer: In a circular linked list, the last node points back
to the first node, forming a loop. This structure facilitates
easy traversal from the last node to the first.

CLICK ME HERE

You might also like