# Intersecting Lines - Facebook Top Interview Questions

### Problem Statement :

```You are given a two-dimensional list of integers lines and integers lo and hi.

Each element in lines contains [m, b] which represents the line y = mx + b.

Return the number of lines that intersect with at least one other line between x = lo and x = hi, inclusive.

Constraints

0 ≤ n ≤ 100,000 where n is the length of lines

Example 1

Input

lines = [

[2, 3],

[-3, 5],

[4, 6]

]

lo = 0

hi = 1

Output

2

Explanation

Lines [2, 3] and [-3, 5] intersect at x = 0.4, which is between x = 0 and x = 1.

Example 2

Input

lines = [

[-1, 0],

[-1, 1],

[-1, 2],

[-1, 3]

]

lo = 0

hi = 1

Output

0

Explanation

None of the lines intersect anywhere.```

### Solution :

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

void eval(vector<pair<long long, long long>>& ret, vector<pair<long long, long long>>& lines,
int x) {
ret.clear();
for (int i = 0; i < lines.size(); i++) {
ret.emplace_back(lines[i].first * x + lines[i].second, i);
}
sort(ret.begin(), ret.end());
}

int solve(vector<vector<int>>& olines, int lo, int hi) {
vector<pair<long long, long long>> lines;
for (auto& out : olines) lines.emplace_back(out[0], out[1]);
vector<pair<long long, long long>> lhs, rhs;
eval(lhs, lines, lo);
eval(rhs, lines, hi);
int ret = 0;
map<int, int> dp;
for (int i = 0; i < lhs.size();) {
int j = i + 1;
while (j < lhs.size() && lhs[i].first == lhs[j].first) j++;
for (int k = i; k < j; k++) {
dp[lhs[k].second] = j - 1;
}
i = j;
}
multiset<int> seen;
int reallhs = 0;
for (int i = 0; i < rhs.size();) {
int j = i + 1;
while (j < rhs.size() && rhs[j].first == rhs[i].first) j++;
for (int k = i; k < j; k++) {
seen.insert(dp[rhs[k].second]);
}
if (reallhs + seen.size() - 1 == *seen.rbegin()) {
if (seen.size() > 1) ret += seen.size();
reallhs += seen.size();
seen.clear();
}
i = j;
}
assert(seen.size() == 0);
return ret;
}```
```

```                        ```Solution in Java :

import java.util.*;

class Solution {
public int solve(int[][] lines, int lo, int hi) {
long[][] p = new long[lines.length][2];
for (int i = 0; i < lines.length; i++) {
p[i][0] = lines[i][0] * lo + lines[i][1];
p[i][1] = lines[i][0] * hi + lines[i][1];
}
Arrays.sort(p, new Comparator<long[]>() {
public int compare(long[] a, long[] b) {
if (a[0] == b[0])
return Long.compare(b[1], a[1]);
return Long.compare(a[0], b[0]);
}
});
long max = p[0][1], min = p[p.length - 1][1];
HashSet<Integer> set = new HashSet();
for (int i = 1; i < p.length; i++) {
if (p[i][1] <= max)
max = Math.max(max, p[i][1]);
}
for (int i = p.length - 2; i > -1; i--) {
if (p[i][1] >= min)
min = Math.min(min, p[i][1]);
}
return set.size();
}
}```
```

```                        ```Solution in Python :

class Solution:
def solve(self, lines, lo, hi):
segments = [(m * lo + b, m * hi + b, i) for i, (m, b) in enumerate(lines)]
segments.sort()
res = [0 for _ in lines]

cstarting = Counter([a for a, b, i in segments])
for (x, y, i) in segments:
if cstarting[x] > 1:
res[i] = 1

curmax = -(10 ** 10)
prevx = -(10 ** 10)
for (x, y, i) in segments:
if x == prevx:
res[i] = 1
if y <= curmax:
res[i] = 1
curmax = max(curmax, y)
prevx = x

curmin = 10 ** 10
prevx = 10 ** 10

for (x, y, i) in segments[::-1]:
if x == prevx:
res[i] = 1
if y >= curmin:
res[i] = 1
curmin = min(curmin, y)
prevx = x

return sum(res)```
```

## Insert a Node at the Tail of a Linked List

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink

Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below

## Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

## Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

## Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing