# Kth User to Visit Website- Google Top Interview Questions

### Problem Statement :

```You are given a two-dimensional list of integers requests and an integer k.

Each element requests[i] contains [start, end] representing an inclusive interval for the possible times when user i visited the website.

Each user visits the site exactly once.

Return the users who may have been the kth (0-indexed) user to visit the site, sorted in ascending order. You can assume that the site can serve an unlimited number of users at any time.

Constraints

1 ≤ n ≤ 100,000 where n is the length of requests

0 ≤ k < n

Example 1

Input

requests = [

[3, 4],

[1, 3],

[4, 4]

]

k = 1

Output

[0, 1, 2]

Explanation

If users visited the site at timestamps [3, 3, 4], then user 0 and 1 may have been the kth user. If they
visited the site at [4, 3, 4], then user 0 and 2 may have been the kth user.

Example 2

Input

requests = [

[0, 0],

[0, 0],

[1, 1],

[2, 2],

[2, 2]

]

k = 3

Output

[3, 4]

Explanation

The only possible timestamps are [0, 0, 1, 2, 2]. Only the last two users could've been the 3rd user to
visit the site.

Example 3

Input

requests = [

[60, 70],

[0, 10],

[11, 30],

[22, 40]

]

k = 1

Output

[2, 3]

Explanation

If users visited the site at these timestamps [60, 5, 21, 22], then user 2 would've been the kth user. If they visited the site at [60, 5, 23, 22], then 3 would've been the kth user.```

### Solution :

```                        ```Solution in C++ :

bool cmo(vector<int> &a, vector<int> &b) {
return a[1] < b[1];
}

vector<int> solve(vector<vector<int>> &re, int k) {
for (int i = 0; i < re.size(); i++) {
re[i].push_back(i);
}
sort(re.begin(), re.end());
int tmin = re[k][0];
sort(re.begin(), re.end(), cmo);
int tmax = re[k][1];
// cout<<tmin<<" "<<tmax<<endl;
vector<int> res;
for (auto &t : re) {
if (t[0] <= tmax && t[1] >= tmin) {
res.push_back(t[2]);
}
}
sort(begin(res), end(res));

return res;
}```
```

```                        ```Solution in Java :

import java.util.*;

class Solution {
public int[] solve(int[][] requests, int k) {
long l = 0, h = Integer.MAX_VALUE;
while (l <= h) {
long m = (l + h) >>> 1;
if (canByEnd(requests, m, k + 1))
h = m - 1;
else
l = m + 1;
}
int end = (int) l;
l = 0;
h = Integer.MAX_VALUE;
while (l <= h) {
long m = (l + h) >>> 1;
if (canByStart(requests, m, k + 1))
h = m - 1;
else
l = m + 1;
}
int start = (int) l;
List<Integer> poss = new ArrayList<>();
for (int i = 0; i != requests.length; i++) {
int[] r = requests[i];
if (r[0] > end || r[1] < start)
continue;
// if ((r[0] <= start && r[1] >= start) || (r[0] <= end && r[1] >= end))
}
Collections.sort(poss);
int[] res = new int[poss.size()];
for (int i = 0; i != poss.size(); i++) res[i] = poss.get(i);
return res;
}

boolean canByStart(int[][] reqs, long time, final int K) {
int count = 0;
for (int[] r : reqs)
if (r[0] <= time)
count++;
return count >= K;
}

boolean canByEnd(int[][] reqs, long time, final int K) {
int count = 0;
for (int[] r : reqs) {
if (r[1] <= time)
count++;
}
return count >= K;
}
}```
```

```                        ```Solution in Python :

class Solution:
def solve(self, requests, k):
# calculate the earliest and latest visit times for user k.
# return all requests that overlap with this interval.
ks = sorted(s for s, e in requests)[k]
ke = sorted(e for s, e in requests)[k]
return [i for i, (s, e) in enumerate(requests) if not (ke < s or e < ks)]```
```

## View More Similar Problems

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that their order is reversed. The head pointer given may be null meaning that the initial list is empty. Example: head references the list 1->2->3->Null. Manipulate the next pointers of each node in place and return head, now referencing the head of the list 3->2->1->Null. Function Descriptio

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

## Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

## Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

## Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

## Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer