RaRa Delivery
10+ M O J & Associates Interview Questions and Answers
Q1. DSA and Language Questions: 1. Difference between Arrays and ArrayList in Java. 2. Queue Implementation using Linked List. 3. BST- How would you fill a BST with a sorted array. 4. Random pointer linked-list clo...
read moreA list of technical questions related to data structures and algorithms in Java.
Arrays are fixed in size while ArrayLists can dynamically grow and shrink.
Queue can be implemented using a linked list by adding elements to the end and removing from the front.
To fill a BST with a sorted array, we can recursively divide the array in half and add the middle element as the root.
Random pointer linked-list clone can be done by creating a new node for each node in the original list an...read more
Q2. During Binary search, what if negative elements were there in an Array as well how would you search a specific element and time complexity for the same.
Negative elements in array won't affect binary search. Time complexity remains O(log n).
Binary search works by dividing the array into two halves and comparing the middle element with the target element.
If the middle element is greater than the target, search in the left half, else search in the right half.
Negative elements won't affect this process as long as the array is sorted.
Time complexity remains O(log n) as the search space is halved in each iteration.
Q3. Two uniform wires totally burnt in a total of 30 minutes. How will you measure 45 minutes?
Burn one wire at both ends and the other wire at one end. When the first wire burns out, light the other end of the second wire.
Burn one wire at both ends and the other wire at one end
When the first wire burns out, light the other end of the second wire
The second wire will burn for 15 minutes, totaling 45 minutes
Q4. BST- How will you fill a BST with a sorted Array?
To fill a BST with a sorted array, we can use a recursive approach.
Find the middle element of the array and make it the root of the BST
Recursively construct the left subtree using the left half of the array
Recursively construct the right subtree using the right half of the array
Q5. How does Binary search be done in a rotated array?
Binary search in a rotated array can be done by finding the pivot point and then applying binary search on the two subarrays.
Find the pivot point by comparing mid element with the first and last elements of the array
Apply binary search on the two subarrays formed by the pivot point
Repeat until the element is found or the subarray is empty
Time complexity is O(log n)
Example: [4,5,6,7,0,1,2], target=0. Pivot point is 3. Binary search on [4,5,6,7] and [0,1,2]
Q6. Print Fibonacci numbers until the nth term using only recursion (no loop allowed)
Printing Fibonacci numbers using recursion only
Define a recursive function that takes two arguments, n and a list to store the Fibonacci sequence
Base case: if n is 0 or 1, return the list
Recursive case: append the sum of the last two elements in the list to the list and call the function with n-1
Call the function with n and an empty list to start the sequence
Print the list of Fibonacci numbers
Q7. Puzzles: 1. Plane and Fuel Puzzle 2. Two uniform wires completely burned in a total of 30 mins. How will you measure 45 mins
Answering two puzzles - Plane and Fuel, and Two Wires Burned
For the Plane and Fuel puzzle, calculate the amount of fuel needed for half the journey and then double it
For the Two Wires Burned puzzle, light one wire at both ends and the other wire at one end only after the first wire has burned out halfway
Both puzzles require creative thinking and problem-solving skills
Q8. What is the fastest sorting algorithm?
The fastest sorting algorithm is QuickSort.
QuickSort has an average time complexity of O(n log n).
It is a divide and conquer algorithm that recursively partitions the array.
It is widely used in practice and has many variations such as randomized QuickSort.
Other fast sorting algorithms include MergeSort and HeapSort.
Q9. Difference between Arrays & ArrayLists in Java?
Arrays are fixed in size while ArrayLists can dynamically grow or shrink.
Arrays are of fixed size while ArrayLists can be resized dynamically.
Arrays can hold primitive data types while ArrayLists can only hold objects.
Arrays are faster than ArrayLists for accessing elements.
ArrayLists have built-in methods for adding, removing, and sorting elements.
Example: int[] arr = new int[5]; ArrayList
list = new ArrayList<>();
Q10. OOPS: 1. Polymorphism with example 2. Overloading vs overriding
Polymorphism is the ability of an object to take on many forms. Overloading is having multiple methods with the same name but different parameters. Overriding is having a method in a subclass with the same name and parameters as a method in the superclass.
Polymorphism allows objects to be treated as if they are of different types. For example, a parent class reference can be used to refer to a child class object.
Overloading is used to provide different implementations of a me...read more
Q11. Clone a linked list with a random pointer.
Clone a linked list with a random pointer.
Create a new node for each node in the original list
Store the mapping of original node to new node in a hash table
Set the random pointer of each new node based on the mapping
Traverse the original list and the new list simultaneously to set the next pointers
Q12. Fibonacci number generation using recursion.
Fibonacci number generation using recursion
Define a function that takes an integer as input
If the input is 0 or 1, return the input
Else, return the sum of the function called with input-1 and input-2
Call the function with the desired input
Q13. Queue Implementation using Linked List?
Queue can be implemented using a singly linked list where insertion happens at the tail and deletion at the head.
Create a Node class with data and next pointer
Create a Queue class with head and tail pointers
Enqueue operation: create a new node and add it to the tail of the list
Dequeue operation: remove the node at the head of the list and update the head pointer
Peek operation: return the data at the head of the list without removing it
Q14. Show the Array Rotation
Array rotation is the process of shifting the elements of an array to the left or right.
To rotate an array to the left, move the first element to the end of the array and shift the remaining elements to the left.
To rotate an array to the right, move the last element to the beginning of the array and shift the remaining elements to the right.
The number of rotations can be specified by the user.
Example: If the array is [1, 2, 3, 4, 5] and we want to rotate it to the left by 2 p...read more
Q15. Show reflection in java.
Reflection in Java allows inspection and modification of runtime behavior of a program.
Reflection is achieved through classes in the java.lang.reflect package.
It allows access to class information, constructors, methods, and fields at runtime.
Reflection can be used to create new objects, invoke methods, and access or modify fields.
Example: Class c = Class.forName("java.lang.String");
Example: Method m = c.getDeclaredMethod("substring", int.class);
Q16. Print pair with given sum.
Given an array of integers and a target sum, find a pair of integers that add up to the target sum.
Create a hash table to store the difference between the target sum and each element in the array
Iterate through the array and check if the current element is present in the hash table
If it is, return the current element and its corresponding hash table value as the pair that adds up to the target sum
If no such pair is found, return null or an empty array
Q17. Polymorphism with Example
Polymorphism is the ability of an object to take on many forms. It allows objects of different classes to be treated as if they were of the same class.
Polymorphism is achieved through method overriding and method overloading.
Method overriding is when a subclass provides its own implementation of a method that is already provided by its parent class.
Method overloading is when a class has two or more methods with the same name but different parameters.
Example of polymorphism: A...read more
Q18. Overloading vs Overriding
Overloading is having multiple methods with the same name but different parameters. Overriding is having a method in the subclass with the same name and parameters as in the superclass.
Overloading is compile-time polymorphism while overriding is runtime polymorphism.
Overloading is used to provide different ways of calling the same method while overriding is used to provide a specific implementation of a method in the subclass.
Overloading is achieved within the same class whil...read more
Top Sde1 Interview Questions from Similar Companies
Reviews
Interviews
Salaries
Users/Month