Professional Documents
Culture Documents
In what scenario would you prefer using a Stack instead of an ArrayList or LinkedList in Java?
----When recursion needs to be implemented
In what scenario would you prefer using a Queue instead of an ArrayList or LinkedList in Java?
---- When elements need to be processed in the order they were added
If the user inputs 99 and 66. What will be the output of the sample program?
---- Enter 2 numbers: 99 66
Priority queue elements before deletion:
[66, 99]
Removing two numbers from the priority queue:
Priority queue elements after deletion:
[99]
Priority queue elements after deletion:
[1, 99]
Priority queue elements after peek():
1
What is the significance of the PriorityQueue class in Java compared to other data structures?
---- It orders elements based on their natural ordering or according to a comparator
Which of the following represents the final arrangement of the numbers [45, 12, 22, 44, 32] when
inserted consecutively using the PriorityQueue algorithm?
----[12, 32, 22, 45, 44]
How does choosing an appropriate data structure affect the performance of algorithms?
---- It can significantly improve or degrade algorithm performance
Design an algorithm to check if a given number is prime. Describe the steps involved in your
algorithm and explain why it is efficient.
---- Start by dividing the number by 2 and continue checking divisibility up to its square root. If
no divisors are found, the number is prime. This algorithm is efficient because it only checks
up to the square root of the number, reducing the number of iterations needed.
Which of the following statements best describes an abstract data type (ADT)?
---- An ADT is a conceptual model that defines the operations that can be performed on a data
structure and the properties of those operations.
Consider the following function that takes reference to head of a Doubly Linked List as parameter.
Assume that a node of doubly linked list has previous pointer as prev and next pointer as next.
if(temp != NULL )
*head_ref = temp->prev;
}
Assume that reference of head of following doubly linked list is passed to above function 1 <--> 2 <--
> 3 <--> 4 <--> 5 <-->6. What should be the modified linked list after the function call?
Answer: 6 < - - > 5 < - - > 4 < - - > 3 < - - > 2 < - - > 1
The following function reverse() is supposed to reverse a singly linked list. There is one line missing
at the end of the function.
/* Link list node */
struct node
{
int data;
struct node* next;
};
What should be added in place of "/*ADD A STATEMENT HERE*/", so that the function correctly
reverses a linked list.
----*head_ref = prev;
The following C function takes a simply-linked list as input argument. It modifies the list by moving
the last element to the front of the list and returns the modified list. Some part of the code is left
blank. Choose the correct alternative to replace the blank line.
What are the time complexities of finding 8th element from beginning and 8th element from end in a
singly linked list? Let n be the number of nodes in linked list, you may assume that n > 8.
--- O(1) and O(n)
Is it possible to create a doubly linked list using only one pointer with every node?
--- Yes, possible by storing XOR of addresses of previous and next nodes.
Linked lists in Data Structures and Algorithms have the following characteristics, EXCEPT for one:
----- Deletion is O (1)
For a given linked list p, the function f returns 1 if and only if _____
-- The elements in the list are sorted in non-decreasing order of data value
It is the most useful for tasks that can be defined in terms of similar subtasks.
---Recursion
In what scenario would you prefer using a Stack instead of an ArrayList or LinkedList in Java?
A) When elements need to be accessed randomly
B) When elements need to be inserted at the beginning
C) When recursion needs to be implemented
D) When elements need to be sorted in ascending order
Which of the following Java code snippets correctly creates a Queue object using
java.util.LinkedList?
A) Queue<Integer> queue = new Queue<>();
B) Queue<Integer> queue = new LinkedList<>();
C) Queue<Integer> queue = new ArrayList<>();
D) Queue<Integer> queue = new LinkedList<Integer>();
In what scenario would you prefer using a Queue instead of an ArrayList or LinkedList in Java?
A) When elements need to be accessed randomly
B) When elements need to be inserted at the beginning
C) When elements need to be processed in the order they were added
D) When elements need to be sorted in ascending order
What does the stack.peek() method do in the context of the given Java program?
A) Adds an element to the top of the stack
B) Removes the top element of the stack
C) Returns the top element of the stack without removing it
D) Checks if the stack is empty
What is the significance of using a Stack data structure in the context of algorithms or applications?
A) It guarantees constant-time access to its elements
B) It ensures elements are processed in the order they are added
C) It supports concurrent access by multiple threads
D) It provides efficient searching and sorting operations
Which of the following represents the final arrangement of the numbers [3, 1, 2, 5, 4] when inserted
consecutively using the PriorityQueue algorithm?
a. [1, 2, 3, 4, 5] b. [5, 4, 3, 2, 1]
c. [1, 3, 2, 5, 4] d. [4, 5, 2, 1, 3]
How does choosing an appropriate data structure affect the performance of algorithms?
A) It has no impact on algorithm performance
B) It can significantly improve or degrade algorithm performance
C) It only affects the readability of the algorithm
D) It determines the algorithm's compatibility with different programming languages
How does the choice of data structure impact the efficiency of algorithms?
A) The data structure has no impact on algorithm efficiency.
B) The data structure can significantly affect the time and space complexity of algorithms.
C) Algorithms are independent of data structures and operate at a constant efficiency.
D) The choice of data structure affects only the readability of the algorithm, not its efficiency.
Suppose you must design an algorithm to find the maximum element in an array. Describe the
steps you would take to create this algorithm.
A) Identify the problem, break it into smaller subproblems, design a step-by-step solution, and
analyze its efficiency.
B) Start writing code immediately, without planning or considering different approaches.
C) Use trial and error to develop the algorithm, testing different strategies until one works.
D) Consult existing algorithms and modify them slightly to suit your specific requirements.
Design an algorithm to check if a given number is prime. Describe the steps involved in your
algorithm and explain why it is efficient.
A) Start by dividing the number by 2 and continue checking divisibility up to its square root. If
no divisors are found, the number is prime. This algorithm is efficient because it only checks
up to the square root of the number, reducing the number of iterations needed.
B) Generate all possible divisors of the number and check each one individually. If no divisors other
than 1 and the number itself are found, the number is prime. This algorithm is efficient because it
exhaustively searches for divisors, ensuring accuracy.
C) Multiply the number by itself repeatedly until it exceeds the given number. If the result matches
the given number, the number is prime. This algorithm is efficient because it relies on simple
arithmetic operations.
D) Use a brute-force approach to check divisibility by every integer from 2 to the given number
minus one. If no divisors are found, the number is prime. This algorithm is efficient because it
covers all possible cases without skipping any potential divisors.
In a queue data structure, which operation removes an element from the front end?
A) push() B) enqueue()
D) isEmpty() C) dequeue()
Why might a doubly linked list be preferred over a singly linked list for certain applications?
A) Doubly linked lists have a simpler implementation than singly linked lists.
B) Doubly linked lists require less memory compared to singly linked lists.
C) Doubly linked lists allow for bidirectional traversal and easier removal of elements.
D) Doubly linked lists have better performance for search operations than singly linked lists.
Which of the following points is/are true about Linked List data structure when it is compared with
array?
A) Arrays have better cache locality that can make them better in terms of performance.
B) It is easy to insert and delete elements in Linked List
C) Random access is not allowed in a typical implementation of Linked Lists and the size of
array has to be pre-decided, linked lists can change their size any time.
D) All of the above
Is it possible to create a doubly linked list using only one pointer with every node?
A) Not possible
B) Yes, possible by storing XOR of addresses of previous and next nodes.
C) Yes, possible by storing XOR of current node and next node
D) Yes, possible by storing XOR of current node and previous node