## Flipkart Interview Question for SDE-2s

• 0

Country: India
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
0
of 0 vote

I would use a red-black tree the key being the login time, then write an additional function rangeQuery which collects the users. Red black tree because it is balanced and therefore faster. In the rangeQuery function we keep adding till we hit the upper bound, but we have to traverse a part of the left tree as well if the root is greater than the lower bound.

Comment hidden because of low score. Click to expand.
0
of 0 vote

For any range relate queries, Segment tree data structure is helpful.

Comment hidden because of low score. Click to expand.
0
of 0 vote

Height balanced Interval Tree

Comment hidden because of low score. Click to expand.
0
of 0 vote

segment tree will do the job

Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <bits/stdc++.h>
#define M 25
using namespace std;
int tree[4*M+1][2];
int timein[25];
int timeout[25];
struct node
{
};
void build(int node ,int start,int end)
{
if(start==end)
{
tree[node][0] += timein[start];
tree[node][1] += timeout[start];
}
else{
int mid = (start+end)/2;
build(2*node,start,mid);
build(2*node+1,mid+1,end);

tree[node][0] = tree[2*node][0]+tree[2*node+1][0];
tree[node][1] = tree[2*node][1]+tree[2*node+1][1];
}
}
struct node *query(int node,int start,int end,int l,int r)
{
struct node *p1,*p2,*ans;
ans = (struct node *)malloc(sizeof(struct node));
if(r<start || end<l || start>end)
{
ans->logout = 0;
return ans;
}
if(l<=start && end<=r)
{
ans->logout = tree[node][1];
return ans;
}
int mid = (start+end)/2;
p1 = query(node*2, start, mid, l, r);
p2 = query(node*2+1, mid + 1, end, l, r);
ans->logout = p1->logout + p2->logout;
return ans;
}
int main()
{
scanf("%d",&n);
for(i=0;i<n;i++){
timeout[logout]++;
}
build(1,0,24);
int q;
struct node *ans;
scanf("%d",&q);
while(q--){
int l,r;
scanf("%d%d",&l,&r);
ans = query(1,0,24,l,r);
printf("Total No. of Users LoggedIn=>%d & Loggedout=>%d in b/w [%d,%d]\n",ans->login,ans->logout,l,r);
}
return 0;
}

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.