Questions
Questions
Q1. Chef visited a grocery store for supplies. There are N items in the store where the ith item has a
freshness value Ai, and cost Bi. Chef has decided to purchase all the items having a freshness value
greater than equal to X. Find the total cost of the groceries Chef buys.
Sample test case: Input: N2, X20, A[15, 67], B = [10, 90]
Output: 90 (Explanation: Item 2 has freshness value greater than equal to X-20. Thus, Chef buys item
2. The total cost is 90.)
Q2. We need to find the total number of steps need to make the current array of size N same as an
array containing 1 to N numbers as elements. Each decrement or increment is counted as a step.
Sample test case: Input: A [8, 3, 2, 1, 9]
Output: 8 (Explanation: As our target array is [1,2,3,4,5], we already have 1, 2, and 3 in our array. We
have 8 and 9 in place of 4 and 5, so if convert 8 to 4, and 9 to 5, we can achieve target array. Hence
minimum increments/decrements required will be (8-4)+(9-5) ways i.e. 8 steps.)
Q3. Given the head of a linked list, Find the number of critical points. (The starting and end are not
considered critical points). Local minima or maxima are called critical points. A Node is called a local
minima if both next and previous elements are greater than the current element. A Node is called a
local maxima if both next and previous elements are smaller than the current element.
Sample test case: Input: L [1, 2, 3, 2, 1, 3, 2)
Output: 3 (Explanation: 3rd node, 5th node and 6th node are the critical nodes, hence the answer is 3)
Q1. Write a Java program to identify the missing number in a sequence of integers ranging from 1 to n.
The input will be an array of size (n-1), containing distinct integers from 1 to n with one number
missing. The program should output the missing number.
Example: Input: Array = [1, 2, 4, 6, 3, 7, 8] Output: Missing number = 5
Q2. You are given a building with n floors and a special ball. When the ball dropped from a floor, the
ball may either:
1. Break if dropped from a floor equal to or higher than a critical floor (F).
2. Remain intact if dropped from a floor below the critical floor (F).
Write a Java program to determine the critical floor (F) with a minimum number of drops.
Input:
Number of floors (n): 100
Critical floor (F): 30th
Output: Total drops required: 7
Q3. You are given two singly linked lists: List¹ and List2.
Write a Java function to insert List2 into List1 after the second element of List1. If List1 has less than
two elements, append List2 at the end of Listl.
Input:
List1=1>2>3-4-> NULL List2-56 -> NULL
Output:
1-2-5-6-3-4-> NULL
Classification: Confidential
Q1. There is an integer array A sorted in ascending order (with distinct values). Prior to being passed to
your function, A is possibly rotated at an unknown pivot index k (1 <=k< nums.length) such that the
resulting array is [nums[k], nums[k+1],..., nums[n-1], nums[0], nums[1]..... nums/k-11] (0-indexed).
For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2], Given the
array A after the possible rotation and an integer target, return the index of target if it is in A, or -1 if it
is not in A. You must write a java program with O(log n) runtime complexity.
Q2. Given an integer array A, move all 0's to the end of it while maintaining the relative order of the
non-zero elements. Note that you must do this in-place without making a copy of the array. Write a
java program to implement it. Example: Input: A = [0,1,0,3,12] Output: [1,3,12,0,0]
Q3.Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem
without modifying the values in the list's nodes (i.e., only nodes themselves may be changed). Given
the head of a linked list, reverse the nodes of the list k at a time, and return the modified list. k is a
positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a
multiple of k then left-out nodes, in the end, should remain as it is. You may not alter the values in the
list's nodes, only nodes themselves may be changed. Write a java program to implement it. For
example Input: head = [1,2,3,4,5], k=2 Output: [2,1,4,3,5)
Q1. During a security guard interview, each candidate's height is measured against a standard height
'h' A value that indicates the deviation from the standard height, either Ofthe samel or a ve (more than
standard) or ve (less than standardi, is retained for subsequent processing. Which height is most
common? (h and all deviation vahies are considered as integers).
Case Study: For 15 candidates with standard height h-6, the list showing deviation in height is (1, 0, -1,
-2, 1, 2, 2, 1, 1, 1, 1, 0, -1, -1, 1). The most common height in 7.
Q2. You are given a list of 'n' students with their names and total marles secured in the examination.
Write a program to arrange them in non-increasing order of their marks and decide if a unique or joint
topper exists. Display the name of the topper/toppers,
Q3. Two linked lista, 'A' and B', were given. A is expected to be a list of even numbers, and B is
expected to be a lat of odd numbers. But in the input, 'A' has one odd element, and 'B' has an even
element. Write a program to create linked lists and a method to operate on these two lists to update
them as expected.
Test case:
Input: (A: 150-52-26-31-12->64] and [B: 23->100->7-17-3) Output: A: 150->52-26->100->12->64)
and (B: 23-531-7-17-3)
Classification: Confidential
int totalCost = 0;
for (int i = 0; i < N; i++) {
if (A[i] >= X) {
totalCost += B[i];
}
}
return totalCost;
}
return steps;
}
System.out.println(minSteps(A)); // Output: 8
Classification: Confidential
}
}
—————
public class ArrayTransformation {
public static int minSteps(int[] A) {
int N = A.length;
int steps = 0;
return steps;
}
System.out.println(minSteps(A)); // Output: 8
}
}
—————
public class MissingNumberFinder {
public static int findMissingNumber(int[] array, int n) {
// Calculate the expected sum of numbers from 1 to n
int expectedSum = n * (n + 1) / 2;
Classification: Confidential
// The missing number is the difference between the expected
sum and the actual sum
return expectedSum - actualSum;
}
if (mid == F) {
return drops;
} else if (mid < F) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return drops;
}
Classification: Confidential
int n = 100; // Number of floors
int F = 30; // Critical floor
// If list1 has less than two elements, append list2 at the end of
list1
if (count < 2) {
current.next = list2;
} else {
// Insert list2 after the second element of list1
ListNode temp = current.next;
current.next = list2;
Classification: Confidential
list2 = list2.next;
}
return list1;
}
—————
public class RotatedSortedArraySearch {
public static int search(int[] nums, int target) {
int left = 0;
Classification: Confidential
int right = nums.length - 1;
if (nums[mid] == target) {
return mid;
}
————
Classification: Confidential
public class MoveZeroes {
public static void moveZeroes(int[] nums) {
int lastNonZeroFoundAt = 0;
moveZeroes(nums);
——
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
this.next = null;
}
}
Classification: Confidential
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0, head);
ListNode pre = dummy;
ListNode cur = head;
while (cur != null && cur.next != null) {
ListNode t = cur.next;
cur.next = t.next;
t.next = cur;
pre.next = t;
pre = cur;
cur = cur.next;
}
return dummy.next;
}
}
——-
import java.util.HashMap;
import java.util.Map;
Classification: Confidential
}
}
return mostCommonHeight;
}
——————
import java.util.*;
Classification: Confidential
} else {
break; // Exit loop once marks are no longer equal
}
}
Classification: Confidential
// Constructor
public Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
}
—————
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
this.next = null;
}
}
Classification: Confidential
// Swap the values of the misplaced elements
if (oddInA != null && evenInB != null) {
int temp = oddInA.val;
oddInA.val = evenInB.val;
evenInB.val = temp;
}
}
Classification: Confidential
System.out.print("List B: ");
printList(headB); // Output: List B: 23 -> 31 -> 7 -> 17 -> 3 ->
NULL
}
}
Classification: Confidential