Questions Arrays LinkedLists
Questions Arrays LinkedLists
Question 1: Traversal
Scenario: You have an array representing the daily earnings of a store over a week. You
need to find out the total earnings
Array:
int n = 7;
Task: Write a function to traverse the array and calculate the total earnings for the first three
days.
Expected Output:
Question 2: Insertion
Scenario: You have an array representing the scores of a game. A new score needs to be
added to the end of the array.
Array:
int n = 3;
Task: Write a function to insert the new score into the array.
Expected Output:
Question 3: Deletion
Scenario: You have an array representing a series of task durations in minutes. One of the
tasks is cancelled, and you need to remove its duration from the array.
Array:
int n = 4;
Task: Write a function to delete the specified task duration from the array.
Expected Output:
Scenario: You have an array of temperatures recorded hourly over a day. A new reading
needs to be inserted at a specific hour.
Array:
int n = 4;
int position = 2;
Task: Write a function to insert the new temperature at the specified position in the array.
Expected Output:
Array:
int n = 5;
Task: Write a function to traverse the array and print the indices where the scores are above
75.
Expected Output:
Linked Lists
You are managing a linked list representing the scores of players in a game. Perform various
operations on this linked list based on the given scenarios.
Base Code for Linked List Creation
#include <stdio.h>
#include <stdlib.h>
int main() {
// Initial linked list creation (empty)
struct Node* head = NULL;
return 0;
}
Task: Create a linked list with the scores: 50, 60, 70, 80, 90. Write a function to traverse and
print the linked list.
Example:
int main() {
struct Node* head = createNode(50);
head->next = createNode(60);
head->next->next = createNode(70);
head->next->next->next = createNode(80);
head->next->next->next->next = createNode(90);
printList(head);
return 0;
}
Expected Output:
50 -> 60 -> 70 -> 80 -> 90 -> NULL
Task: Insert a new score, 100, at the end of the linked list.
if (*head == NULL) {
*head = newNode;
return;
}
last->next = newNode;
}
int main() {
// Initial linked list creation
struct Node* head = createNode(50);
head->next = createNode(60);
head->next->next = createNode(70);
head->next->next->next = createNode(80);
head->next->next->next->next = createNode(90);
addScore(&head, 100);
printList(head);
return 0;
}
Expected Output:
50 -> 60 -> 70 -> 80 -> 90 -> 100 -> NULL
Example:
void removeScore(struct Node** head, int scoreToRemove) {
struct Node* temp = *head, *prev = NULL;
prev->next = temp->next;
free(temp);
}
int main() {
// Initial linked list creation
struct Node* head = createNode(50);
head->next = createNode(60);
head->next->next = createNode(70);
head->next->next->next = createNode(80);
head->next->next->next->next = createNode(90);
head->next->next->next->next->next = createNode(100);
removeScore(&head, 70);
printList(head);
return 0;
}
Expected Output:
50 -> 60 -> 80 -> 90 -> 100 -> NULL
Task: Insert a new score, 75, at the third position (0-based index) in the linked list.
Example:
if (position == 0) {
newNode->next = *head;
*head = newNode;
return;
}
int main() {
// Initial linked list creation
struct Node* head = createNode(50);
head->next = createNode(60);
head->next->next = createNode(80);
head->next->next->next = createNode(90);
head->next->next->next->next = createNode(100);
return 0;
}
Expected Output:
50 -> 60 -> 75 -> 80 -> 90 -> 100 -> NULL
Task: Write a function to traverse the linked list and return the sum of scores that are above
70.
Example:
int main() {
// Initial linked list creation
struct Node* head = createNode(50);
head->next = createNode(60);
head->next->next = createNode(75);
head->next->next->next = createNode(80);
head->next->next->next->next = createNode(90);
head->next->next->next->next->next = createNode(100);
return 0;
}
Expected Output:
Sum of scores above 70: 345