OA Questions
OA Questions
Q2
https://www.thejoboverflow.com/p/p2074/
#include <bits/stdc++.h>
The maximum of arr[0..n-1] should come in first partition. The max of rest of the array should
come in the next partition.
#include<bits/stdc++.h>
int main(){
int n;
cin >> n;
vector<int> arr;
int top = -1;
while(!pq.empty()){
auto cur = pq.top(); pq.pop();
if(top < cur.second){
arr.push_back(cur.second);
top = cur.second;
}
}
int m = arr.size();
vector<int> dp(m, 1);
Oracle
1 Female Codebreaker
TheJobOverflow | Oracle, New coding question | Female Codebreaker | 2023
Maximum GCD among all subarrays of size >= 2 - Stack Overflow
#include<bits/stdc++.h>
int main(){
int n;
cin >> n;
vector<int> arr(n);
gcds[n - 1] = INT_MIN;
int maxgcd = 1;
for(int i = 0; i < n; i++) maxgcd = max(maxgcd, gcds[i]);
int l = 0, r = 0, ans = 2;
2 Subsequence Sort
https://www.thejoboverflow.com/p/p1879/
Possible solution:->
#include <bits/stdc++.h>
using namespace std;
vector<int>fun(vector<string>&arr,string cur){
int cur_zero = 0, cur_one = 0, cur_score = 0, n= arr.size();
for(int i =cur.length();i>=0;i--){
if(cur[i]=='0')cur_zero++;
if(cur[i]=='1'){
cur_score+=cur_zero;
cur_one++;
}
}
vector<int>ans(n,0);
for(int i = 0;i<n;i++){
int j = arr[i].length()-1;
int zero_count = 0;
int one_count = 0;
for(int j = arr[i].length()-1;j>=0;j--){
if(arr[i][j]=='0')zero_count++;
else if(arr[i][j]=='1') one_count++;
}
zero_count = cur_zero-zero_count;
one_count = cur_one-one_count;
if(zero_count<0||one_count<0)break;
for(int j =0;j<arr[i].length();j++){
if(arr[i][j]!='?')continue;
if(zero_count>0){arr[i][j]='0';zero_count--;}
else {arr[i][j]='1';one_count--;}
}
int score = 0;
for(int j = arr[i].length()-1;j>=0;j--){
if(arr[i][j]=='0')zero_count++;
else score+=zero_count;
}
if(cur_score >= score)ans[i]=1;
}
return ans;
}
int main()
{
int n = 2;
vector<string>arr = {"?110?1","111???"};
string start = "110100";
auto ans = fun(arr,start);
for(auto it:ans){if(it==1)cout<<"YES"<<" ";else cout<<"NO";}
return 0;
}
=
Please review for example string - {0,1} =binary
{?,0} = target
#include<bits/stdc++.h>
int main(){
int n;
int slen;
string binary;
vector<string> arr(n);
for(int i = 0; i < n; i++) cin >> arr[i];
q = queue<int>();
for(int j = slen - 1; j >= 0; j--){
if(arr[i][j] == '0' && binary[j] == '1')
q.push(j);
else if(arr[i][j] == '?' && binary[j] == '1')
q.push(j);
}
3 Untitled Question 2
https://www.thejoboverflow.com/p/p1300/
#include<bits/stdc++.h>
int main(){
int n = 5;
vector<int> arr = {1, 3, 5, 2, 10};
ans = total - (right - left) * (right - left) + (mid - left) * (mid - left)
+ (right - mid) * (right - mid);
4 Untitled Question 3
https://www.thejoboverflow.com/p/p1300/
#include<bits/stdc++.h>
int main(){
int n, x, m, q;
cin >> n >> x >> m >> q;
sort(logs.begin(), logs.end());
sort(queries.begin(), queries.end());
int l = 0, r = 0;
vector<int> ans(q);
ans[queries[i].second] = n - map.size();
}
5 Dominating XOR
https://www.thejoboverflow.com/p/p1300/
#include<bits/stdc++.h>
//
// a XOR b > a AND b if a and b have their first set bit from MSB at different
position
// 00010101011111
// 00000101010111
// XOR 00010000001000
// AND 00000101010111
// if they have their first set bit at same position, XOR will have 0, and AND
will have 1
// at that position => AND > XOR
int main(){
int n = 4;
vector<int> arr = {1, 1, 5, 7};
vector<int> firstbit(n);
int ans = 0;
https://www.thejoboverflow.com/p/p1300/
https://practice.geeksforgeeks.org/problems/minimum-swaps/1
https://www.geeksforgeeks.org/minimum-number-swaps-required-sort-array/
int minSwaps(vector<int>&nums)
{
int n = nums.size();
vector<pair<int, int>> ds(n);
for(int i = 0; i < n; i++) ds[i] = {nums[i], i};
sort(ds.begin(), ds.end());
vector<int> graph(n);
vector<bool> visited(n);
int ans = 0;
}
}
return ans;
}
7 Rest API
https://www.thejoboverflow.com/p/p1413/
import requests
ans = {}
def processData():
print("Processing data...")
url = "https://jsonmock.hackerrank.com/api/universities"
page = 1
while True:
res = requests.get(url=url, params = {"page": page})
data = res.json()
data = data["data"]
if len(data) == 0:
break
if curcountry in ans.keys():
if ans[curcountry]["rank"] > currank:
ans[curcountry] = {"name": curname, "rank": currank}
else:
ans[curcountry] = {"name" : curname, "rank": currank}
page += 1
print("Done")
def bestUniversityByCountry(country):
if country in ans.keys():
return ans[country]["name"]
return ""
def main():
processData()
print(bestUniversityByCountry(country))
if __name__ == "__main__":
main()
Samsung
1.Unique subsequence
#include <iostream>
#include <vector>
#include <map>
using namespace std;
const int MOD = 1000000007;
int countDistinctSubsequences(string s) {
int n = s.length();
vector<int> dp(n + 1, 0);
map<char, int> lastOccurrence;
dp[0] = 1; // Empty subsequence is counted
for (int i = 1; i <= n; i++) {
dp[i] = (2 * dp[i - 1]) % MOD;//twice piche wala add karenge
//check kro ke last pehle occur hua h kya
if (lastOccurrence.find(s[i - 1]) != lastOccurrence.end()) {
//kaha hua h use value minus krdo dp ki
// s[i-1] wo character dega and lastOccurrence[ch] wo index jo
dp ko chiye
dp[i] -= dp[lastOccurrence[s[i - 1]]];
}
dp[i] = (dp[i] + MOD) % MOD;
lastOccurrence[s[i - 1]] = (i - 1);//last character jo aya usko add
krdo
}
return (dp[n] - 1 + MOD) % MOD; // Subtract 1 to exclude the empty
subsequence
}
int main() {
string s = "aabc";
int result = countDistinctSubsequences(s);
cout << "Number of distinct subsequences: " << result << endl;
return 0;
}
2 Word wrap
Last line cost was included in the actual question, but on gfg last line cost is not included.
Remove the postsum thing and the code should work for company
https://practice.geeksforgeeks.org/problems/word-wrap1646/
return ans;
}
#include<bits/stdc++.h>
if(r < n && r >= 0 && c < m && c >= 0 && matrix[x][y] + 1 ==
matrix[r][c]){
int temp = dfs(r, c, matrix, cost);
curans = max(curans, temp);
}
}
cost[x][y] = 1 + curans;
ans = max(ans, cost[x][y]);
return cost[x][y];
}
int main() {
int n, m;
cin >> n >> m;
char ch;
cin >> ch;
#include<bits/stdc++.h>
int main() {
int n;
cin >> n;
sort(ds.begin(), ds.end());
int ans = 0, maxsofar = ds[n - 1].second;
Stripe
1 Payout
struct record{
int dd, mm, yy;
int amount;
string currency;
};
return amt;
}
if(acc.find(charges[i].currency) == acc.end()){
charges[i].amount = convert(charges[i].currency, defaultcur,
charges[i].amount);
charges[i].currency = defaultcur;
}
map[charges[i].currency] += charges[i].amount;
}
// fee
for(auto keyval: map){
map[keyval.first] = keyval.second * 0.98;
}
stringstream ss (temp);
ss >> charges[i].yy >> dump >> charges[i].mm >> dump >> charges[i].dd >>
dump;
ss >> charges[i].amount >> dump;
ss >> charges[i].currency;
}
1.2 Part 2
#include<bits/stdc++.h>
using namespace std;
struct record{
int dd, mm, yy;
double amount;
string currency;
};
return amt;
}
if(m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
return 31;
return 30;
}
return string(temp);
}
if (amt == 0) return;
if(acc.find(cur) == acc.end()){
amt = convert(cur, defaultcur, amt);
cur = defaultcur;
}
string processed_date;
if(cur == "USD"){
processed_date = getDateStr(yy, mm, dd, 0);
usd[processed_date] += amt * 0.98;
}
else if(cur == "CHF"){
processed_date = getDateStr(yy, mm, dd, 3);
chf[processed_date] += amt * 0.98;
}
else if(cur == "EUR"){
processed_date = getDateStr(yy, mm, dd, 2);
eur[processed_date] += amt * 0.98;
}
else if(cur == "GBP"){
processed_date = getDateStr(yy, mm, dd, 1);
gbp[processed_date] += amt * 0.98;
}
}
vector<string> output;
if(part == "one"){
printPartOne(usd, "USD", output);
printPartOne(chf, "CHF", output);
printPartOne(eur, "EUR", output);
printPartOne(gbp, "GBP", output);
}
else{
printPartTwo(usd, "USD", output);
printPartTwo(chf, "CHF", output);
printPartTwo(eur, "EUR", output);
printPartTwo(gbp, "GBP", output);
}
sort(output.begin(), output.end());
stringstream ss (temp);
ss >> charges[i].yy >> dump >> charges[i].mm >> dump >> charges[i].dd >>
dump;
ss >> charges[i].amount >> dump;
ss >> charges[i].currency;
}
Meesho
1 Interesting pairs
https://www.thejoboverflow.com/p/p1508/
#include<bits/stdc++.h>
using namespace std;
// sorting array will ensure arr[i] <= arr[j] hence (arr[i] - arr[j]) <= 0
// solving the exp => 2 * arr[j] = samval
int main(){
int n, samval;
cin >> n;
vector<int> arr(n);
sort(arr.begin(), arr.end());
int ans = 0;
2 Visible profile
https://www.thejoboverflow.com/p/p1508/
#include<bits/stdc++.h>
using namespace std;
class DisjointSet{
vector<int> parent, setsize;
public:
DisjointSet(int n){
parent.resize(n);
setsize.resize(n, 1);
int main(){
int n, m, q;
cin >> n >> m >> q;
int from[n], to[n], queries[n];
3 Meesho OA question 1
https://drive.google.com/drive/u/5/folders/1-vtRHN3R1ZOYg38ae21Y84frS-Ev1ERT
#include<bits/stdc++.h>
int main(){
int n;
cin >> n;
vector<int> prices(n);
4 Meesho Question 2
#include<bits/stdc++.h>
using namespace std;
ans += kv.second - 1;
}
}
int i = 0;
while(i < n && !pq.empty()){
int curval = pq.top().first, f = pq.top().second;
pq.pop();
Solution 2
vector<int>fun(vector<int>&arr){
unordered_map<int,int>mp; vector<int>ans;
for(int i=0;i<arr.size();i++){
if(mp.count(arr[i])){
if(ans.size()==0||ans.back()<=arr[i]){
ans.push_back(arr[i]);
}
else{
return {-1};
}
}
mp[arr[i]]++;
}
return ans;
}
5 Meesho Q3
https://www.codechef.com/problems/PRESUFOP
Solution 1
Not sure :wrong testcase{5,3,6,3,5}
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] source = new int[n];
int[] target = new int[n];
for(int i=0;i<n;i++) source[i] = sc.nextInt();
for(int i=0;i<n;i++) target[i] = sc.nextInt();
System.out.println(count(n, source, target));
}
public static int count(int n, int[] source, int[] target){
int[] req = new int[n];
for(int i=0;i<n;i++){
req[i] = target[i] - source[i];
if(req[i] < 0) return -1;
}
int descend_left = 1;
int descend_right = 1;
for(int i=1;i<n;i++){
if(req[i] <= req[i-1]) descend_left++;
else break;
}
for(int i=n-2;i>=0;i--){
if(req[i] <= req[i+1]) descend_right++;
else break;
}
if(descend_left + descend_right < n) return -1;
if(descend_left == n) return req[0];
if(descend_right == n) return req[n-1];
return req[0]+req[n-1];
}
}
Solution 3
Bhai aftab wrong solution aa rha is ka bhi
Not sure
#include<bits/stdc++.h>
ans += cur;
right += cur;
}
return ans;
}
int main(){
int n;
cin >> n;
}
//code chef sa tapa ha
# cook your dish here def presufop():
for t in range(int(input())):
n = int(input()) a = list(map(int, input().split()))
b = list(map(int, input().split()))
op1 , op2 = b[0]-a[0], 0
res1, res2 = 0, 0
for i in range(n):
if (op1 + op2 < b[i] - a[i]):
op2 += ((b[i]-a[i]) - op1 - op2)
elif (op1 + op2 > b[i] - a[i]):
op1 -= (op1 + op2 - (b[i] - a[i]))
if (op1 < 0 or op2 > (b[n-1] - a[n-1])):
res1 = -1 res2 = 0
break
res1 = max(res1, op1)
res2 = max(res2, op2)
print(res1 + res2)
if __name__ == "__main__":
presufop()
6 Flipkart
1 . Online Video streaming platform
https://www.thejoboverflow.com/p/p1638/
Print an integer representing the minimum number of iterations of swapping required so that at
least K videos of identical length get the contiguous position. If the required arrangement is not
possible then print -1.\
Sol 1
#include <bits/stdc++.h>
using namespace std;
int fun(vector<int>&arr,int k){
unordered_map<int,pair<int,int>>mp;
unordered_map<int,queue<int>>pos;
if(k==1)return 0;
int ans = INT_MAX;
int n = arr.size();
for(int i=0;i<n;i++){
if(mp.count(arr[i])){
auto prev = mp[arr[i]];
int cur_cost = i-prev.first-1+prev.second;
pos[arr[i]].push(i);
if(pos[arr[i]].size()>k){
int ind = pos[arr[i]].front();
pos[arr[i]].pop();
int reduce_cost = pos[arr[i]].front()-ind-1;
cur_cost-=k*reduce_cost;
}
if(pos[arr[i]].size()==k){
ans = min(ans,cur_cost);
}
mp[arr[i]] = {i,cur_cost};
}
else{
mp[arr[i]] = {i,0};
pos[arr[i]].push(i);
}
}
unordered_map<int,pair<int,int>>mp1;
unordered_map<int,queue<int>>pos1;
for(int i=n-1;i>=0;i--){
if(mp1.count(arr[i])){
auto prev = mp1[arr[i]];
int cur_cost = prev.first -1 -i + prev.second;
pos1[arr[i]].push(i);
if(pos1[arr[i]].size()>k){
int ind = pos1[arr[i]].front();
pos1[arr[i]].pop();
int reduce_cost = ind-1-pos1[arr[i]].front();
cur_cost -= k*reduce_cost;
}
if(pos1[arr[i]].size()==k){
ans = min(ans,cur_cost);
}
mp1[arr[i]] = {i,cur_cost};
}
else{
mp1[arr[i]] = {i,0};
pos1[arr[i]].push(i);
}
}
return ans;
}
int main() {
int n;
cin>>n;
vector<int>arr(n);
for(int i =0;i<n;i++){
int temp;
cin>>temp;
arr[i]=temp;
}
int k;
cin>>k;
int ans = fun(arr,k);
cout<<ans<<" \n";
}
Sol 2
https://www.thejoboverflow.com/p/p1638/
Only 2 is >= 4
Bring all 2 around the 2 at index 2
#include<bits/stdc++.h>
using namespace std;
// first window
int mid = (l + r) / 2, cur = 0, ans = INT_MAX;
int median = ds[mid].second;
int t1 = l;
while(t1 <= r){
int targetpos = median - (mid - t1);
cur += abs(targetpos - ds[t1].second);
t1++;
}
mid++;
median = ds[mid].second;
return ans;
}
int main(){
int n, k;
cin >> n;
vector<int> arr(n, 0);
for(int i = 0; i < n ; i++) cin >> arr[i];
cin >> k;
vector<pair<int, int>> ds(n);
sort(ds.begin(), ds.end());
ds.push_back({INT_MAX, -1});
int l = 0, r = 1, ans = INT_MAX;
while(r < n + 1){
if(ds[l].first != ds[r].first){
int temp;
if(r - l >= k){
temp = solve(ds, l, r - 1, k);
ans = min(ans, temp);
}
l = r;
}
r++;
}
cout << ans << endl;
return 0;
}
2 Warehouse robot
https://drive.google.com/drive/u/5/folders/1pZGjZxlYyB-x3LR-_xGjfJ8vUdjZxdBK
Smallest range in K lists | Practice | GeeksforGeeks
https://www.youtube.com/watch?v=mYDF7xsYmI4
while(!pq.empty()){
int val = pq.top().first;
int row = pq.top().second;
pq.pop();
}
return ans;
}
Air India
1 Racer Selection
https://drive.google.com/drive/u/5/folders/10ssHI8WVn1CaaF7YfxZ5aCOw4GUhqFcS
int solve2(vector<int>& arr, int n, int k){
int start = 0, end = 0, ans = INT_MIN;
unordered_map<int, int> map;
multiset<int> ms;
return ans;
}
int main(){
int n, k;
cin >> n >> k;
vector<int> arr(n);
for(int i = 0; i < n; i++) cin >> arr[i];
1 Special Subsequence
https://drive.google.com/drive/u/5/folders/11PO_wCeJWiuRG7PV85qVzfwCvqIXo5XN
#include<bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
if(r > n - r)
r = n - r;
if(b % 2 == 1)
return (a * t * t) % mod;
return (t * t) % mod;
}
for(char c : s){
freq[c - 'a']++;
}
return ans;
}
int main(){
int n, k;
cin >> n >> k;
string s;
cin >> s;
cout << solve(n, k, s) << endl;
}
2 More ones
https://practice.geeksforgeeks.org/problems/count-the-substring--170645/1
https://www.youtube.com/watch?v=LO1qGTE-Xbo
https://drive.google.com/drive/u/5/folders/11PO_wCeJWiuRG7PV85qVzfwCvqIXo5XN
#include<bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
while(!pq.empty()){
int val = pq.top().first;
int row = pq.top().second;
pq.pop();
pq.push({arr[row][index[row]], row});
maxx = max(maxx, arr[row][index[row]]);
index[row]++;
}
return ans;
}
int main(){
int r, c;
cin >> r >> c;
Capital one
1 Matchmaker
https://drive.google.com/drive/u/5/folders/1R8bfO915uXqA7AQCLigqJFy56EXKDSrw
int main(){
int n, k;
cin >> n >> k;
vector<int> arr(n);
solve(n, k, arr);
}
2 JSON Parsing
https://drive.google.com/drive/u/5/folders/1R8bfO915uXqA7AQCLigqJFy56EXKDSrw
"Student":{"Name":{"FN":"John","LN":"Hussain"},"Age":"15"},"Class":"10"
3
Student.Name.LN Student.Age Class
#include<bits/stdc++.h>
using namespace std;
for(char c: key){
targetcode = (targetcode * 26 + c) % mod;
}
int l = 0, r = 0;
while(r < n){
sourcecode = (sourcecode * 26 + json[r]) % mod;
if(r - l + 1 < k) {
r++;
continue;
}
if(sourcecode == targetcode){
if(json.substr(l, k) == key){
if(json[r + 3] == '{'){
int t = r + 4, count = 1;
while(t < n && count != 0){
if(json[t] == '{') count++;
else if(json[t] == '}') count--;
t++;
}
return json.substr(r + 3, (t - 1)- (r + 3) + 1);
}
else if (json[r + 3] == '"'){
int t = r + 4;
while(t < n && json[t] != '"') t++;
if(sourcecode < 0)
sourcecode += mod;
l++;
r++;
}
//Tokenizing on '.'
while(getline(ss, temp, '.')){
value = getValue(temp, value);
}
ans[i] = value;
}
int main(){
string text;
cin >> text;
int q;
cin >> q;
vector<string> queries(q);
#include<bits/stdc++.h>
using namespace std;
void build(vector<pair<int, int>>& seg, vector<int>& arr, int ind, int left,
int right){
if(left > right) return;
if(left == right){
seg[ind] = {arr[left - 1], 1};
return;
}
int update(vector<pair<int, int>>& seg, int ind, int offset, int k){
int toreturn;
if(offset + seg[ind].second == k && seg[ind].first != -1){
toreturn = seg[ind].first;
seg[ind].first = -1;
seg[ind].second--;
}
else if(offset + seg[ind].second >= k){
toreturn = update(seg, 2 * ind + 1, offset, k);
seg[ind].second--;
}
else
toreturn = update(seg, 2 * ind + 2, offset + seg[ind].second, k);
return toreturn;
}
int main(){
int n;
cin >> n;
}
4 Hit counts
#include<bits/stdc++.h>
using namespace std;
return 0;
}
int main(){
int k, th, n;
cin >> k >> th >> n;
vector<int> arr(n);
#include<bits/stdc++.h>
int main(){
int mod = 1e9 + 7;
int n, q;
cin >> n >> q;
return 0;
5 A binary Palindrome
#include<bits/stdc++.h>
using namespace std;
return ans;
}
int main(){
long long arr[100001];
arr[0] = 1; arr[1] = 3;
int k = 2;
int t;
cin >> t;
while(t--){
int n;
cin >> n;
AMD
1 Rotate Matrix 90 deg
https://practice.geeksforgeeks.org/problems/rotate-by-90-degree-1587115621/1
https://drive.google.com/file/d/1ejC_HoYavwVp5TIaGsQZm1DoqhJZ6vYl/view?usp=drive_link
// swapping rows
int l = 0, r = n - 1;
while(l < r){
for(int i = 0; i < n; i++)
swap(matrix[l][i], matrix[r][i]);
l++; r--;
}
}
2 Count triplet with sum less than X
https://www.geeksforgeeks.org/count-triplets-with-sum-smaller-that-a-given-value/
// (i, j, k) triplets
return ans;
}