Deloitte_Coding
Deloitte_Coding
1. What is System.out.println()?
2. What is the difference between Abstract class, and Interface? Can we create a constructor
of both?
3. Can we have different written types in method overloading and overriding?
4. Implement a program to find the factorial of a number using recursion.
5. Write code to reverse a linked list.
6. Solve a mathematical or logical aptitude question involving patterns or series.
7. Design an algorithm to solve a problem related to data structures, such as sorting or
searching.
8. Write a program that takes an octal number as input and converts it to binary.
9. Write a program to identify the node at which two linked lists merge.
10. Create a program that swaps two given numbers without using an extra variable.
11. Write a program to rotate a matrix by 90 degrees in the clockwise direction.
12. Implement Kadane’s Algorithm to determine the maximum sum of a contiguous subarray.
13. Write a program to verify if two given strings are anagrams.
14. Given an array with numbers from 1 to n, find the missing number.
15. Write a program to find the longest substring in a string that is a palindrome.
16. Rotate an array by k positions.
17. Write a program to find the spiral traversal of a matrix.
18. Solve a problem involving string manipulation or pattern matching.
19. Implement a binary search algorithm in Java and discuss its time complexity.
20. Explain the concept of dynamic programming and provide an example problem.
21. Solve a problem involving tree traversal or manipulation, such as finding the lowest
common ancestor.
22. Design a class hierarchy for a banking system encompassing accounts, transactions, and
customers.
1. What is System.out.println()?
System.out.println() is a method in Java used to print output to the console. The System class is part
of the java.lang package, and out is a static instance of PrintStream. The println() method prints the
message followed by a new line.
2. Difference between Abstract class and Interface? Can we create a constructor for both?
• Abstract Class: Can have both abstract (without implementation) and concrete methods
(with implementation). A class can inherit from only one abstract class due to single
inheritance in Java.
• Interface: Only contains abstract methods (until Java 8, after which default methods with
implementation were introduced). A class can implement multiple interfaces due to multiple
inheritance.
Constructors:
• Abstract class: Can have constructors, which are invoked when the class is subclassed.
• Overriding: No, the return type must be the same or co-variant (in case of inheritance).
java
if (n == 0) return 1;
int num = 5;
}
5. Write code to reverse a linked list
java
class LinkedList {
Node head;
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
newNode.next = head;
head = newNode;
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
temp = temp.next;
list.push(20);
list.push(15);
list.push(10);
list.push(5);
list.printList();
list.reverse();
list.printList();
Example question: Find the next number in the series 1, 4, 9, 16, ? The numbers are squares of
natural numbers. The next number is 25.
java
public class PatternSeries {
7. Design an algorithm to solve a problem related to data structures, such as sorting or searching
java
int n = arr.length;
arr[j] = arr[j+1];
arr[j+1] = temp;
bubbleSort(arr);
System.out.println("Sorted array:");
8. Write a program that takes an octal number as input and converts it to binary
java
import java.util.Scanner;
int decimal = 0, i = 0;
while (octal != 0) {
++i;
octal /= 10;
return Integer.toBinaryString(decimal);
}
9. Write a program to identify the node at which two linked lists merge
java
class LinkedList {
Node head;
class Node {
int data;
Node next;
// Move the pointer of the longer list to the same starting point
} else {
ptr1 = ptr1.next;
ptr2 = ptr2.next;
}
int length = 0;
length++;
head = head.next;
return length;
10. Create a program that swaps two given numbers without using an extra variable
java
a = a + b;
b = a - b;
a = a - b;
swap(a, b);
java
int n = matrix.length;
matrix[j][n - i - 1] = temp;
rotateMatrix(matrix);
System.out.println();
12. Implement Kadane’s Algorithm to determine the maximum sum of a contiguous subarray
java
return maxSoFar;
java
import java.util.Arrays;
if (str1.length() != str2.length()) {
return false;
Arrays.sort(arr1);
Arrays.sort(arr2);
14. Given an array with numbers from 1 to n, find the missing number
java
int arrSum = 0;
for (int num : arr) {
arrSum += num;
int n = 6;
15. Write a program to find the longest substring in a string that is a palindrome
java
start = i - (len - 1) / 2;
end = i + len / 2;
}
l--;
r++;
return r - l - 1;
String s = "babad";
java
int n = arr.length;
reverse(arr, 0, n - 1);
reverse(arr, 0, k - 1);
reverse(arr, k, n - 1);
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
int k = 3;
rotate(arr, k);
java
rowStart++;
colEnd--;
rowEnd--;
colStart++;
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
printSpiral(matrix);
18. Solve a problem involving tree traversal or manipulation, such as finding the lowest common
ancestor.
Solution in Java:
java
class TreeNode {
int val;
return (left != null && right != null) ? root : (left != null ? left : right);
19. Implement a binary search algorithm in Java and discuss its time complexity.
Solution:
java
20. Explain the concept of dynamic programming and provide an example problem.
Dynamic programming (DP) solves problems by breaking them into smaller subproblems and storing
solutions to avoid redundant calculations.
java
dp[0] = 0; dp[1] = 1;
return dp[n];
System.out.println(fibonacci(10)); // Output: 55
java
import java.util.Arrays;
Arrays.sort(arr1);
Arrays.sort(arr2);
22. Design a class hierarchy for a banking system encompassing accounts, transactions, and
customers.
java
class Customer {
@Override
@Override
class Transaction {