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 :



title-img




                        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))
            poss.add(i);
        }
        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

Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

View Solution →

Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

View Solution →

Square-Ten Tree

The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the

View Solution →

Balanced Forest

Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a

View Solution →

Jenny's Subtrees

Jenny loves experimenting with trees. Her favorite tree has n nodes connected by n - 1 edges, and each edge is ` unit in length. She wants to cut a subtree (i.e., a connected part of the original tree) of radius r from this tree by performing the following two steps: 1. Choose a node, x , from the tree. 2. Cut a subtree consisting of all nodes which are not further than r units from node x .

View Solution →

Tree Coordinates

We consider metric space to be a pair, , where is a set and such that the following conditions hold: where is the distance between points and . Let's define the product of two metric spaces, , to be such that: , where , . So, it follows logically that is also a metric space. We then define squared metric space, , to be the product of a metric space multiplied with itself: . For

View Solution →