Tip 1: Never leave any topic from any chapter/subject.
Tip 2: Learn to explain your thoughts well.
Tip 3: Learn from previous experiences/interviews/problems asked.
Tip 4: Include at least four projects on your resume.
Tip 1: Include at least four projects on your resume.
Tip 2: Do not write false information. You will always get caught. Be genuine.
Simple DSA round taken by a very supportive interviewer. He asked about two DSA problems.
An array c is a subarray of array d if c can be obtained from d by deletion of several elements from the beginning and several elements from the end.
For e.g.- The non-empty subarrays of an array [1,2,3] will be- [1],[2],[3],[1,2],[2,3],[1,2,3].
If arr = {-3,4,5}.
All the possible non-empty contiguous subarrays of “arr” are {-3}, {4}, {5}, {-3,4}, {4,5} and {-3,4,5}.
The product of these subarrays are -3, 4, 5, -12, 20 and -60 respectively.
The maximum product is 20. Hence, the answer is 20.
Can you solve this in linear time and constant space complexity?
The code defines a function named maxSubarrayProduct that takes an integer array A and its size n as arguments.
It initializes a variable r with the first element of the array A. This variable will store the maximum subarray product found so far.
The function then enters a loop that starts from the second element of the array A and goes up to the last element.
Inside the loop, the function initializes two variables imax and imin with the value of r. These variables will store the maximum and minimum product of subarrays that end with the current number A[i].
If the current number A[i] is negative, the function swaps imax and imin. This is because multiplying a negative number with a maximum product will give a minimum product and vice versa.
The function then calculates the maximum and minimum product of subarrays that end with the current number A[i]. It does this by taking the maximum and minimum of either the current number A[i] or the maximum and minimum product of subarrays that end with the previous number times the current number.
The function then updates the value of r to the maximum of r and imax. This is because the newly computed maximum value is a candidate for the global maximum subarray product.
After the loop ends, the function returns the value of r.
The code defines a main function that initializes an array arr with some values, calls the maxSubarrayProduct function passes the array and its size as arguments, and then prints the result to the console.
The program then exits with a status of 0.
The given linked list is 1 -> 2 -> 3 -> 4-> NULL. Then the reverse linked list is 4 -> 3 -> 2 -> 1 -> NULL and the head of the reversed linked list will be 4.
Can you solve this problem in O(N) time and O(1) space complexity?
Initialize three pointers prev as NULL, curr as head, and next as NULL.
Iterate through the linked list. In a loop, do the following:
Before changing the next of curr, store the next node
next = curr -> next
Now update the next pointer of curr to the prev
curr -> next = prev
Update prev as curr and curr as next
prev = curr
curr = next
This was a round to test your JAVA knowledge as I applied for this role. He asked me a lot of questions on JAVA and my past projects followed by 2 DSA questions.
Given 'N' : 5 (number of packets) and 'M' : 3 (number of students)
And chocolates in each packet is : {8, 11, 7, 15, 2}
All possible way to distribute 5 packets of chocolates among 3 students are -
( 8,15, 7 ) difference of maximum-minimum is ‘15 - 7’ = ‘8’
( 8, 15, 2 ) difference of maximum-minimum is ‘15 - 2’ = ‘13’
( 8, 15, 11 ) difference of maximum-minimum is ‘15 - 8’ = ‘7’
( 8, 7, 2 ) difference of maximum-minimum is ‘8 - 2’ = ‘6’
( 8, 7, 11 ) difference of maximum-minimum is ‘11 - 7’ = ‘4’
( 8, 2, 11 ) difference of maximum-minimum is ‘11 - 2’ = ‘9’
( 15, 7, 2 ) difference of maximum-minimum is ‘15 - 2’ = 13’
( 15, 7, 11 ) difference of maximum-minimum is ‘15 - 7’ = ‘8’
( 15, 2, 11 ) difference of maximum-minimum is ‘15 - 2’ = ‘13’
( 7, 2, 11 ) difference of maximum-minimum is ‘11 - 2’ = ‘9’
Hence there are 10 possible ways to distribute ‘5’ packets of chocolate among the ‘3’ students and difference of combination (8, 7, 11) is ‘maximum - minimum’ = ‘11 - 7’ = ‘4’ is minimum in all of the above.
Initially sort the given array. Declare a variable to store the minimum difference, and initialize it to INT_MAX. Let the variable be min_diff.
Find the subarray of size m such that the difference between the last (maximum in case of sorted) and first (minimum in case of sorted) elements of the subarray is minimum.
We will run a loop from 0 to (n-m), where n is the size of the given array and m is the size of the subarray.
We will calculate the maximum difference with the subarray and store it in diff = arr[highest index] – arr[lowest index]
Whenever we get a diff less than the min_diff, we will update the min_diff with diff.
Input: 'a' = [7, 12, 1, 20]
Output: NGE = [12, 20, 20, -1]
Explanation: For the given array,
- The next greater element for 7 is 12.
- The next greater element for 12 is 20.
- The next greater element for 1 is 20.
- There is no greater element for 20 on the right side. So we consider NGE as -1.
Push the first element to stack.
Pick the rest of the elements one by one and follow the following steps in the loop.
Mark the current element as next.
If the stack is not empty, compare top most element of the stack with the next.
If next is greater than the top element, Pop the element from the stack. next is the next greater element for the popped element.
Keep popping from the stack while the popped element is smaller than the next. next becomes the next greater element for all such popped elements.
Finally, push the next in the stack.
After the loop in step 2 is over, pop all the elements from the stack and print -1 as the next element for them.
Here's your problem of the day
Solving this problem will increase your chance to get selected in this company
Which collection class forbids duplicates?