Quest Global Coding Questions and Answers
Sample Quest Global Coding Questions with Solutions
On this page, you will get Sample Quest Global Coding Questions and Answers asked in Technical Interview involved Quest Global Recruitment Process.
Apart from this, at the end of this you will get Faq’s related to Job profiles, Salary Offered, and specific steps of the Recruitment Process of Quest Global.
Sample Quest Global Coding Questions and Answers - Set 1
Question 1 : HR issues
Problem statement -:
Shovon is an HR in a renowned company and he is assigning people to work. Now he is assigning people work in a fashion where if he assigns somework a work of cost 2, the next person will be strictly getting a job with cost equal or more than 2. Given that Shovon’s company has infinite work and a number of employees, how many distributions can be possible. The cost of jobs can go 0 to 9.
Function Description:
Complete the special_numbers function in the editor below. It has the following parameter(s):
Parameters:
| Name | Type | Description |
| N | Integer | The number of depts. |
| arr[ ] | Integer array | The number of employees in each dept.. |
Return: The function must return an INTEGER denoting the sum of answers for all distinct distributions.
Constraints:
- 1 <= n <= 100
- 1 <= arr[i] <= 200
Sample Cases:
- Sample Input 1
2
4
1 - Sample Output 1
725 - Description
The ans if m = 1 is 10, which is all numbers from 0 to 9
The ans for m = 2 is 55
The answer for m = 3 is 220
The answer for m = 4 is 715
So fun(4) + fun(1) = 725
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
#include<bits/stdc++.h>
using namespace std;
int func(int s,int p,int n)
{
if(p==n-1) return 1;
int ans=0;
for(int i=s;i<=9;i++) ans+=func(i,p+1,n); return ans; } int main() { int n,a, ans=0; cin>>n;
vector v(n);
for(int i=0;i<n;i++) { cin>>a;
for(int i=0;i<=9;i++)
ans+=func(i,0,a);
}
cout<<ans;
}
n=int(input()) a1=[] for i in range(n): a1.append(int(input())) dp=[0]*201 dp[1]=10 dp[2]=55 a=[1]*10 i=3 while i<201: s=0 for i1 in range(10): s+=a[i1] a[i1]=s dp[i]+=(s*(10-i1)) dp[i]=dp[i]%((10**9)+7) i+=1 s1=0 for i in a1: s1+=dp[i] s1=s1%((10**9)+7) print(s1)
import java.util.*;
class Main
{
static int func(int s,int p,int n)
{
if(p==n-1) return 1;
int ans=0;
for(int i=s;i<=9;i++)
ans += func(i,p+1,n);
return ans;
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
int ans=0;
for(int i=0; i<n; i++){
int a = sc.nextInt ();
for(int j=0; j <=9; j++)
ans+=func(j,0,a);
}
System.out.println (ans);
}
}
Question 2: Copycat in exam (R->Medium)
Rahul copies in the exam from his adjacent students. But he doesn’t want to be caught, so he changes words keeping the letter constant. That means he interchanges the positions of letters in words. You are the examiner and you have to find if he has copied a certain word from the one adjacent student who is giving the same exam, and give Rahul the markings he deserves.
Note that: Uppercase and lowercase are the same.
Input Format:
First line with the adjacent student’s word
Second line with Rahul’s word
Output Format:
0 if not copied
1 if copied
Constraints:
1<=Length of string<=10^6
Sample Input:
CAR
ACR
Sample Output:
1
import java.util.*;
public class Main
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
String s1 = sc.next ();
String s2 = sc.next ();
s1 = s1.toUpperCase ();
s2 = s2.toUpperCase ();
char arr1[] = s1.toCharArray ();
Arrays.sort (arr1);
char arr2[] = s2.toCharArray ();
Arrays.sort (arr2);
String res1 = new String (arr1);
String res2 = new String (arr2);
if (res1.equals (res2))
System.out.println ("1");
else
System.out.println ("0");
}
}
s=input()
s1=input()
s=s.lower()
s=sorted(s)
s1=s1.lower()
s1=sorted(s1)
if s1==s:
print(1)
else:
print(0)
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s2,s1;
cin>>s1>>s2;
transform(s1.begin(),s1.end(),s1.begin(),::tolower);
transform(s2.begin(),s2.end(),s2.begin(),::tolower);
sort(s1.begin(),s1.end());
sort(s2.begin(),s2.end());
cout<< (s2==s1);
}
Question 3: Coin Game
Raman was playing a game, he starts with x coins. Now in every step, he wins and loses and he has to get the money or pay the money as needed. He came in contact with a psychic who can see the future and the Psychic predicted the outcomes after each step. Now Raman wants to start the game with the minimum wage where he doesn’t run out of money. Help Raman to find what money he should start with. The only rule to keep playing is not going in a credit situation.
Input Format:
- First line with n, number of steps in the game
- Next n lines, n integers denoting outcomes of every game. Positive means winning and negative means losing that money.
Output Format:
- One single integer denoting the minimum amount to start with
Constraints:
- Number of steps<=10^9
- -1000<=Money needed in each step<=1000
Sample Input:
4
2
-9
15
2
Sample Output:
7
Explanation:
If he starts with 7 rupees, then after steps : 7 ->9 -> 0-> 15 -> 17.
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++) cin>>a[i];
int sum=0,ans=0;
for(int i=0;i<n;i++)
{
sum+=a[i];
if(sum<1)
{
sum=-sum;
ans+=sum;
sum=0;
}
}
cout<<ans<<endl;
}
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
int sum=0,ans=0;
for(int i=0;i<n;i++)
{
sum+=arr[i];
if(sum<1)
{
sum=-sum;
ans+=sum+1;
sum=1;
}
}
System.out.println(ans);
}
}
n=int(input())
arr=[]
for i in range(n):
arr.append(int(input()))
s,a=0,0
for i in arr:
s=s+i
if(s<1):
a=a+(-1*s)
s=0
print(a)
Question 4 : Array Subarray
Problem Statement :
You are given an array, You have to choose a contiguous subarray of length ‘k’, and find the minimum of that segment, return the maximum of those minimums.
Sample input 0 :
1 → Length of segment x =1
5 → size of space n = 5
1 → space = [ 1,2,3,1,2]
2
3
1
2
Sample output :
3
Explanation :
The subarrays of size x = 1 are [1],[2],[3],[1], and [2],Because each subarray only contains 1 element, each value is minimal with respect to the subarray it is in. The maximum of these values is 3. Therefore, the answer is 3
#include <bits/stdc++.h>
using namespace std;
vector < int > arr;
int prevmin = -1;
int flag = 0;
int x, n, q;
int sorting(int start, int end) {
if (start + 1 == n) {
start = 0;
end = end - n;
}
if (start == end) return arr[start];
return min(arr[start], sorting(start + 1, end));
}
int func(int start, int end) {
if (flag == 0) {
flag++;
return prevmin = sorting(start, end);
}
if (arr[start - 1] == prevmin) return prevmin;
return prevmin = (arr[end] <= prevmin) ? prevmin : sorting(start, end); } int main() { cin >> x >> n;
int ans = 0;
for (int i = 0; i < n; i++) { cin >> q;
arr.push_back(q);
}
for (int i = 0; i < n; i++) {
ans = max(ans, func(i, i + x - 1));
}
cout << ans;
}
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int n = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i <= n - x; i++) {
min = Integer.MAX_VALUE;
for (int j = i; j < (i + x); j++)
min = Math.min(min, arr[j]);
max = Math.max(min, max);
}
System.out.println(max);
}
}
s = int(input())
n = int(input())
a = []
for i in range(n):
a.append(int(input()))
def min_in_segment(start, end, prev, s, prev_min):
if s == 1:
return a[start]
else:
if prev == -1 or prev_min == -2:
return min(a[start : end + 1])
elif prev_min != -2:
if prev != prev_min:
if a[end] < prev_min:
return a[end]
else:
return prev_min
else:
return min(a[start : end + 1])
msf = -1
prev = -1
prev_min = -2
for i in range(n - s + 1):
new_min = min_in_segment(i, i + s - 1, prev, s, prev_min)
msf = max(msf, new_min)
prev = a[i]
prev_min = new_min
print(msf)
Question 5 : Last student’s ID (R->Medium+)
Problem Statement :
There is an id code that is supposed to be given to all the aspirants of an exam. It is actually a substring of a given string. That means, the authority takes a string and then assigns all the unique substrings to all the students. Suppose there is a string “abcde”, so the ids of the students will be “a”,”b”,”c”,”d”,”e”,’ab”,”abc”,”abcd”,”abcde”,”bc”,”bcd”,”bcde”,”cd”,”cde”,”de”.
The students are standing in a line according to the lexicographic order of their given ids. You have to find out the id of the last student for the given input string from which the ids are generated.
Input Format:
Single line with the id generating string
Output format:
The last id as per lexicographical order
Constraints:
Number of characters in the string<=10^9
Sample Input:
abdc
Sample output:
dc
Explanation:
The last student will be with the id dc. The order will be
abdc
a
ab
abd
abdc
b
bd
bdc
c
d
dc
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
vector < string > v;
for (int i = 0; i < s.length(); i++) {
v.push_back(s.substr(i, s.length() - i)); //cout<<v[i]<<endl;
}
sort(v.begin(), v.end(), greater < string > ());
cout << v[0];
}
import java.util.*;
public class Main {
public static String maxString(char set[]) {
int n = set.length;
String temp = "";
TreeSet < String > list = new TreeSet < > ();
for (int i = 0; i < n; i++) {
temp = "";
for (int j = i; j < n; j++) {
temp = temp + set[j];
list.add(temp);
}
}
return list.last();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
char arr[] = str.toCharArray();
System.out.println(maxString(arr));
}
}
s = input()
j = 1
c = 0
while j < len(s):
if ord(s[c]) < ord(s[j]):
c = j
j += 1
print(s[c:])
Sample Quest Global Coding Questions and Answers - Set 2
Question 1: Self Sufficient
Problem Statement – Abhijeet is one of those students who tries to get his own money by part time jobs in various places to fill up the expenses for buying books. He is not placed in one place, so what he does, he tries to allocate how much the book he needs will cost, and then work to earn that much money only. He works and then buys the book respectively. Sometimes he gets more money than he needs so the money is saved for the next book. Sometimes he doesn’t. In that time, if he has stored money from previous books, he can afford it, otherwise he needs money from his parents.
Now His parents go to work and he can’t contact them amid a day. You are his friend, and you have to find how much money minimum he can borrow from his parents so that he can buy all the books.
He can Buy the book in any order.
Function Description:
Complete the function with the following parameters:
| Name | Type | Description |
| N | Integer | How many Books he has to buy that day. |
| EarnArray[ ] | Integer array | Array of his earnings for the ith book |
| CostArray[ ] | Integer array | Array of the actual cost of the ith book. |
Constraints:
- 1 <= N <= 10^3
- 1 <= EarnArray[i] <= 10^3
- 1 <= CostArray[i] <= 10^3
Input Format:
- First line contains N.
- Second N lines contain The ith earning for the ith book.
- After that N lines contain The cost of the ith book.
Output Format: The minimum money he needs to cover the total expense.
Sample Input 1:
3
[3 4 2]
[5 3 4]
Sample Output 1:
3
Explanation:
At first he buys the 2nd book, which costs 3 rupees, so he saves 1 rupee. Then he buys the 1st book, that takes 2 rupees more. So he spends his stored 1 rupee and hence he needs 1 rupee more. Then he buys the last book.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, ans =0,sum=0;
cin>>n;
vector arr1(n),arr2(n);
for(int i=0;i<n;i++) cin>>arr2[i];
for(int i=0;i<n;i++) cin>>arr1[i];
for(int i=0;i<n;i++) arr2[i]-=arr1[i]; sort(arr2.begin(),arr2.end(),greater());
for(int i=0;i<n;i++)
{
sum+=arr2[i];
if(sum<0)
{ans+=abs(sum);sum=0;}
}
cout<<ans;
}
n=int(input())
L1=[0] *n
L2=[0]*n
for i in range(n):
L2[i]=int(input())
for i in range(n):
L1[i]=int(input())
for i in range(n):
L2[i]=L2[i]-L1[i];
L2.sort()
su=0
ans=0
for i in range(n):
su=su+L2[i]
if su<0:
ans = ans + abs(su)
su=0
print(ans)
import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
int n=sc.nextInt();
int arr1[]= new int[n];
int arr2[]= new int[n];
for(int i=0; i<n; i++)
arr2[i]=sc.nextInt();
for(int i=0; i<n; i++)
arr1[i]=sc.nextInt();
for(int i=0; i<n; i++) arr2[i] -= arr1[i]; Arrays.sort(arr2); int sum=0, ans=0; for(int i=n-1; i>=0; i--)
{
sum+=arr2[i];
if(sum<0)
{
sum = -sum;
ans= ans +sum ;
sum=0;
}
}
System.out.println(ans);
}
}
Question 2: Amusement park
Problem Statement – Akshay loves to go to WONDERLA , an amusement park. They are offering students who can code well with some discount. Our task is to reduce the cost of the ticket as low as possible.
The cost of tickets can be removed by removing the digits from the price given. They will give some k turns to remove the digits from the price of the ticket. Your task is to help Akshay in coding a program that can help him to reduce the cost of a ticket by removing the digits from its price and getting the maximum possible discount.
Note – You cannot make the cost of a ticket zero. For eg -: If the cost of a ticket is 100, and you have 2 turns to reduce the price, the final price will be 1 and not zero.
Constraints:
- 1 <= number of tickets <= 10^5
- 1 <= K < Number of digits in Price of ticket
Input Format for Custom Testing:
- The first line contains a string,Tickets, denoting the given cost of each ticket.
- The next line contains an integer, K, denoting the number of tickets that is to be removed.
Sample Cases:
- Sample Input 1
203
2 - Sample Output 1
0
#include<bits/stdc++.h>
using namespace std;
int smallestNumber (string num, int k)
{
if(num.length()<=k)
return 0;
unordered_map<char,int> pos;
for(int i=0;i < num.length();i++)
{
pos[num[i]]=i;}
string temp=num;
sort(num.begin(),num.end());
string ans=num.substr(0,num.length()-k);
vector < int > v;
for(int i=0;i < ans.length();i++)
v.push_back(pos[ans[i]]);
sort(v.begin(),v.end());
string ret;
for(int i=0;i < v.size();i++) { ret+=temp[v[i]]; } int final=stoi(ret); return final; } int main() { string s; cin >> s;
int k;
cin >> k;
int ans;
cout << smallestNumber(s,k)%(int)(pow(10,9)+7);
return 0;
}
import sys n=input() k=int(input()) n1=len(n) if len(n)<=k: print(0) sys.exit() a='' i=0 while i < (n1-1) and k>0: if int(n[i])>int(n[i+1]): i+=1 k-=1 continue else: a+=n[i] i+=1 a+=n[i] i+=1 if k>0: a=a[:-k] if i<=(n1-1): while i < n1: a+=n[i] i+=1 print(int(a)%((10**9)+7))
import java.util.*;
class Main
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
String s = sc.nextLine ();
int k = sc.nextInt ();
int ans;
System.out.println (smallestNumber (s, k) % (int) (1e9 + 7));
}
static int smallestNumber (String num, int k)
{
if (num.length () <= k)
return 0;
HashMap < Character, Integer > pos = new HashMap <> ();
for (int i = 0; i < num.length (); i++)
{
pos.put (num.charAt (i), i);
}
String temp = num;
num = sortString (num);
String ans = num.substring (0, num.length () - k);
ArrayList < Integer > v = new ArrayList <> ();
for (int i = 0; i < ans.length (); i++)
{
v.add (pos.get (ans.charAt (i)));
}
Collections.sort (v);
String ret = "";
for (int i = 0; i < v.size (); i++)
{
ret += temp.charAt (v.get (i));
}
int result = Integer.parseInt ("" + ret);
return result;
}
public static String sortString (String inputString)
{
char tempArray[] = inputString.toCharArray ();
Arrays.sort (tempArray);
return new String (tempArray);
}
}
Question 3: Majority Element
The majority element in an array is defined as the element that appears more than ⌊n/2⌋ times, where n is the length of the array.
In other words, it is the element that occurs most frequently and makes up more than half of the array.
Given an array of integers, the task is to find the majority element and return it. If there is no majority element, If there is no majority element, the algorithm should indicate that.
Examples:
Example 1:
Input: [3, 3, 4, 2, 4, 4, 2, 4, 4]
Output: 4
Explanation:
In the given array, the number 4 appears 5 times, which is more than half of the array size (9/2 = 4.5). Therefore, 4 is the majority element.
Example 2:
Input: [1, 2, 3, 4, 4, 4, 4]
Output: 4
Explanation:
In this case, the number 4 appears 4 times, which is more than half of the array size (7/2 = 3.5). Thus, 4 is the majority element.
Example 3:
Input: [1, 2, 3, 4, 5]
Output: -1
Explanation:
There is no majority element in this array since no number appears more than half of the array size (5/2 = 2.5).
Example 4:
Input: [2, 2, 2, 3, 3, 4, 4, 4, 4]
Output: -1
Explanation:
In this case, although the number 4 appears 4 times, it does not occur more than half of the array size (9/2 = 4.5).
Hence, there is no majority element.
#include<bits/stdc++.h>
int majorityElement(std::vector& nums) {
int count = 0;
int candidate = 0;
for (int num : nums) {
if (count == 0) {
candidate = num;
count = 1;
} else if (num == candidate) {
count++;
} else {
count--;
}
}
return candidate;
}
int validateMajorityElement(std::vector& nums, int candidate) {
int count = 0;
for (int num : nums) {
if (num == candidate) {
count++;
}
}
if (count > nums.size() / 2) {
return candidate;
} else {
return -1; // No majority element found
}
}
int findMajorityElement(std::vector& nums) {
int candidate = majorityElement(nums);
return validateMajorityElement(nums, candidate);
}
int main() {
std::vector nums = {3, 3, 4, 2, 4, 4, 2, 4, 4};
int result = findMajorityElement(nums);
if (result != -1) {
std::cout << "Majority Element: " << result << std::endl;
} else {
std::cout << "No majority element found." << std::endl;
}
return 0;
}
def majority_element(nums):
count = 0
candidate = None
for num in nums:
if count == 0:
candidate = num
count = 1
elif num == candidate:
count += 1
else:
count -= 1
return candidate
def validate_majority_element(nums, candidate):
count = 0
for num in nums:
if num == candidate:
count += 1
if count > len(nums) // 2:
return candidate
else:
return None
def find_majority_element(nums):
candidate = majority_element(nums)
return validate_majority_element(nums, candidate)
# Example usage
nums = [3, 3, 4, 2, 4, 4, 2, 4, 9]
result = find_majority_element(nums)
if result is not None:
print("Majority Element:", result)
else:
print("No majority element found.")
import java.util.HashMap;
import java.util.Map;
public class Main {
public static int majorityElement(int[] nums) {
int count = 0;
int candidate = 0;
for (int num : nums) {
if (count == 0) {
candidate = num;
count = 1;
} else if (num == candidate) {
count++;
} else {
count--;
}
}
return candidate;
}
public static int validateMajorityElement(int[] nums, int candidate) {
int count = 0;
for (int num : nums) {
if (num == candidate) {
count++;
}
}
if (count > nums.length / 2) {
return candidate;
} else {
return -1; // No majority element found
}
}
public static int findMajorityElement(int[] nums) {
int candidate = majorityElement(nums);
return validateMajorityElement(nums, candidate);
}
public static void main(String[] args) {
int[] nums = {3, 3, 4, 2, 4, 4, 2, 4, 4};
int result = findMajorityElement(nums);
if (result != -1) {
System.out.println("Majority Element: " + result);
} else {
System.out.println("No majority element found.");
}
}
}
Question 4: Smallest window in a string containing all the characters of another string
Given two strings S and P, the task is to find the smallest window in string S that contains all the characters (including duplicates) of string P. If no such window exists, return “-1”. If there are multiple windows of the same length, return the one with the smallest starting index.
Note that all characters are lowercase alphabets.
Example 1:
Input:
S = “timetopractice”
P = “toc”
Output : toprac
Explanation: The smallest substring in S that contains “toc” is “toprac”.
Example 2:
Input:
S = “zoomlazapzo”
P = “oza”
Output:
apzo
Explanation:
The smallest substring in S that contains “oza” is “apzo”.
#include<bits/stdc++.h>
using namespace std;
class WindowFinder {
public:
string findSmallestWindow(string s, string p) {
if (p.length() > s.length()) {
return "-1";
} else {
int sHash[26] = {0};
int pHash[26] = {0};
for (int i = 0; i < p.length(); i++) {
pHash[p[i] - 'a']++;
}
int counter = 0;
int begin = 0;
int startIndex = -1;
int length = 0;
int minLength = INT_MAX;
for (int i = 0; i < s.length(); i++) {
sHash[s[i] - 'a']++;
if (pHash[s[i] - 'a'] != 0 && sHash[s[i] - 'a'] <= pHash[s[i] - 'a']) { counter++; } if (counter == p.length()) { while (sHash[s[begin] - 'a'] > pHash[s[begin] - 'a'] || pHash[s[begin] - 'a'] == 0) {
if (sHash[s[begin] - 'a'] > pHash[s[begin] - 'a']) {
sHash[s[begin] - 'a']--;
}
begin++;
}
length = i - begin + 1;
if (length < minLength) {
startIndex = begin;
minLength = length;
}
}
}
if (startIndex == -1) {
return "-1";
} else {
return s.substr(startIndex, minLength);
}
}
}
};
// Test the code
int main() {
string s = "timetopractice";
string p = "toc";
WindowFinder windowFinder;
string smallestWindow = windowFinder.findSmallestWindow(s, p);
// Print the result
cout << smallestWindow << endl;
return 0;
}
class WindowFinder:
def find_smallest_window(self, s, p):
if len(p) > len(s):
return -1
shash = [0] * 26
phash = [0] * 26
for char in p:
phash[ord(char) - ord('a')] += 1
counter = 0
begin = 0
start_index = -1
length = 0
min_length = float('inf')
for i in range(len(s)):
shash[ord(s[i]) - ord('a')] += 1
if phash[ord(s[i]) - ord('a')] != 0 and shash[ord(s[i]) - ord('a')] <= phash[ord(s[i]) - ord('a')]: counter += 1 if counter == len(p): while shash[ord(s[begin]) - ord('a')] > phash[ord(s[begin]) - ord('a')] or phash[ord(s[begin]) - ord('a')] == 0:
if shash[ord(s[begin]) - ord('a')] > phash[ord(s[begin]) - ord('a')]:
shash[ord(s[begin]) - ord('a')] -= 1
begin += 1
length = i - begin + 1
if length < min_length:
start_index = begin
min_length = length
if start_index == -1:
return "-1"
else:
return s[start_index:start_index + min_length]
s = "timetopractice"
p = "toc"
window_finder = WindowFinder()
smallest_window = window_finder.find_smallest_window(s, p)
print(smallest_window)
public class Main{
public static String findSmallestWindow(String s, String p) {
int len1 = s.length();
int len2 = p.length();
if (len1 < len2) {
return "-1";
}
int[] patHash = new int[256];
int[] strHash = new int[256];
for (int i = 0; i < len2; i++) {
patHash[p.charAt(i)]++;
}
int start = 0;
int startIndex = -1;
int minLength = Integer.MAX_VALUE;
int count = 0;
for (int j = 0; j < len1; j++) {
strHash[s.charAt(j)]++;
if (patHash[s.charAt(j)] != 0 && strHash[s.charAt(j)] <= patHash[s.charAt(j)]) { count++; } if (count == len2) { while (strHash[s.charAt(start)] > patHash[s.charAt(start)] || patHash[s.charAt(start)] == 0) {
if (strHash[s.charAt(start)] > patHash[s.charAt(start)]) {
strHash[s.charAt(start)]--;
}
start++;
}
int windowLen = j - start + 1;
if (minLength > windowLen) {
minLength = windowLen;
startIndex = start;
}
}
}
if (startIndex == -1) {
return "-1";
} else {
return s.substring(startIndex, startIndex + minLength);
}
}
// Test the code
public static void main(String[] args) {
String s = "timetopractice";
String p = "toc";
String smallestWindow = findSmallestWindow(s, p);
// Print the result
System.out.println(smallestWindow);
}
}
Question 5: Nearest smaller Tower
Given an array representing the heights of towers, the task is to find, for each tower, the index of the nearest tower that is shorter than it.
The search for a shorter tower can be performed by looking to the left and right sides of each tower.
The following rules apply:
If there are two or more smaller towers at the same distance from the current tower, choose the tower with the smallest height.
If two towers have the same height, choose the one with the smaller index.
Example 1:
Input : Array: [1, 3, 2]
Output : Indexes: [-1, 0, 0]
Explanation:
For the tower at index 0, there is no tower shorter than it, so the output is -1.
For the tower at index 1 (height 3), there are two towers (heights 1 and 2) at the same distance. Following the rules, we choose the tower with the smallest height, which is at index 0.
For the tower at index 2 (height 2), the only tower shorter than it is at index 0.
Therefore, the final output is the array of indexes: [-1, 0, 0].
Example 2:
Input : Array : [4, 8, 3, 5, 3]
Output : Indexes: [2, 2, -1, 2, -1]
Explanation:
For the tower at index 0 (height 4), the nearest tower shorter than it is at index 2.
For the tower at index 1 (height 8), there are two towers (heights 4 and 3) at the same distance.
Following the rules, we choose the tower at index 2.
For the tower at index 2 (height 3), there is no tower shorter than it.
For the tower at index 3 (height 5), there are two towers (heights 3 and 3) at the same distance.
Following the rules, we choose the tower at index 2 because it has a smaller index.
For the tower at index 4 (height 3), there is no tower shorter than it.
Therefore, the final output is the array of indexes: [2, 2, -1, 2, -1].
#include<bits/stdc++.h>
class TowerFinder
{
public:
std::vector findNearestShorterTower(std::vector &towerHeights)
{
int n = towerHeights.size();
std::stack leftStack, rightStack;
std::vector result(n, -1);
for (int i = 0; i < n; i++) { while (!leftStack.empty() && towerHeights[leftStack.top()] >= towerHeights[i])
{
leftStack.pop();
}
if (!leftStack.empty())
{
result[i] = leftStack.top();
}
leftStack.push(i);
}
for (int i = n - 1; i >= 0; i--)
{
while (!rightStack.empty() && towerHeights[rightStack.top()] >= towerHeights[i])
{
rightStack.pop();
}
if (!rightStack.empty())
{
if (result[i] != -1)
{
if (std::abs(result[i] - i) == std::abs(rightStack.top() - i))
{
if (towerHeights[result[i]] > towerHeights[rightStack.top()])
result[i] = rightStack.top();
}
else if (std::abs(result[i] - i) > std::abs(rightStack.top() - i))
result[i] = rightStack.top();
}
else
result[i] = rightStack.top();
}
rightStack.push(i);
}
return result;
}
};
int main()
{
std::vector towerHeights = {4,8,3,5,3};
TowerFinder towerFinder;
std::vector nearestShorterTowers = towerFinder.findNearestShorterTower(towerHeights);
// Print the result
for (int i = 0; i < nearestShorterTowers.size(); i++)
{
std::cout << nearestShorterTowers[i] << " ";
}
std::cout << std::endl;
return 0;
}
class TowerFinder:
def find_nearest_shorter_tower(self, arr):
suff = []
n = len(arr)
find_suff = [0] * n # building suffix
for i in range(n - 1, -1, -1):
if len(suff) == 0:
find_suff[i] = -1
suff.append([arr[i], i])
else:
while suff:
if suff[-1][0] < arr[i]:
find_suff[i] = suff[-1][1]
break
suff.pop()
if len(suff) == 0:
find_suff[i] = -1
suff.append([arr[i], i])
pre = []
find_pre = [0] * n # building prefix
for i in range(n):
if len(pre) == 0:
find_pre[i] = -1
pre.append([arr[i], i])
else:
while pre:
if pre[-1][0] < arr[i]: find_pre[i] = pre[-1][1] break pre.pop() if len(pre) == 0: find_pre[i] = -1 pre.append([arr[i], i]) new = [0] * n # comparing both for i in range(n): if find_suff[i] == -1: new[i] = find_pre[i] continue if find_pre[i] == -1: new[i] = find_suff[i] continue if abs(find_suff[i] - i) == abs(find_pre[i] - i): if arr[find_suff[i]] >= arr[find_pre[i]]:
new[i] = find_pre[i]
else:
new[i] = find_suff[i]
elif abs(find_suff[i] - i) > abs(find_pre[i] - i):
new[i] = find_pre[i]
else:
new[i] = find_suff[i]
return new
# Test the code
tower_heights = [1, 3, 2]
tower_finder = TowerFinder()
nearest_shorter_towers = tower_finder.find_nearest_shorter_tower(tower_heights)
# Print the result
print(nearest_shorter_towers)
import java.util.*;
class Main {
public static int[] findNearestShorterTower(int[] towerHeights) {
int n = towerHeights.length;
Stack leftStack = new Stack<>();
Stack rightStack = new Stack<>();
int[] result = new int[n];
Arrays.fill(result, -1);
for (int i = 0; i < n; i++) { while (!leftStack.empty() && towerHeights[leftStack.peek()] >= towerHeights[i]) {
leftStack.pop();
}
if (!leftStack.empty()) {
result[i] = leftStack.peek();
}
leftStack.push(i);
}
for (int i = n - 1; i >= 0; i--) {
while (!rightStack.empty() && towerHeights[rightStack.peek()] >= towerHeights[i]) {
rightStack.pop();
}
if (!rightStack.empty()) {
if (result[i] != -1) {
if (Math.abs(result[i] - i) == Math.abs(rightStack.peek() - i)) {
if (towerHeights[result[i]] > towerHeights[rightStack.peek()]) {
result[i] = rightStack.peek();
}
} else if (Math.abs(result[i] - i) > Math.abs(rightStack.peek() - i)) {
result[i] = rightStack.peek();
}
} else {
result[i] = rightStack.peek();
}
}
rightStack.push(i);
}
return result;
}
public static void main(String[] args) {
int[] towerHeights = {4,8,3,5,3};
int[] nearestShorterTowers = findNearestShorterTower(towerHeights);
// Print the result
for (int i = 0; i < nearestShorterTowers.length; i++) {
System.out.print(nearestShorterTowers[i] + " ");
}
System.out.println();
}
}
FAQs on Quest Global Coding Questions and Answers
Question 1: Does Quest Global asks Coding Questions in their Technical Interview ?
Yes, Quest Global asks Data Structures and Algorithms based Coding Questions along with other Core CS Subjects based questions to test the Techical Knowledge of the Candidate.
Question 2: How many rounds are in Quest Global?
Quest Global Recruitment Process for Hiring Freshers includes following steps:
- Online Aptitude + Technical Assessment
- Technical Interview
- HR Round
Question 3: What is the salary of freshers in Quest Global ?
For Software Developer ₹5 – 6 LPA and for Engineer Trainee ₹2 -5 LPA is offered by Quest Global for freshers.
Question 4: What is the eligibilty criteria for hiring freshers in Quest Global ?
For hiring freshers Quest Global the candidate must be:
- B.E or B.Tech Graduate from any stream.
- Minimum score of 60% or equivalent CGPA is required in academics.
- No active backlogs.
