# Ancestor Queries - Google Top Interview Questions

### Problem Statement :

```Implement a data structure with the following methods:

AncestorQuerier(Tree root) which instantiates the data structure with the given tree node. root only contains unique values

int getAncestor(int val, int k) which returns the kth (0-indexed) ancestor of node with value val. If there's
no kth ancestor, return -1.

Constraints

0 ≤ n ≤ 100,000 where n is the number of nodes in root

0 ≤ m ≤ 100,000 where m is the number of calls to getAncestor

Example 1

Input

methods = ["constructor", "getAncestor", "getAncestor", "getAncestor"]

arguments = [[[1, [2, [3, null, null], [4, null, null]], null]], [4, 1], [1, 1], [4, 2]]`

Output

[None, 2, -1, 1]```

### Solution :

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

*/
class AncestorQuerier {
unordered_map<int, int> mp;
vector<vector<int>> pr;

public:
AncestorQuerier(Tree* root) {
int cnt = 0;
getnodes(root, cnt);
pr.resize(cnt + 1, vector<int>(20, -1));
ances(root, NULL);
}

void getnodes(Tree* rt, int& cnt) {
if (rt == NULL) {
return;
}
cnt = cnt + 1;
getnodes(rt->left, cnt);
getnodes(rt->right, cnt);
}

void ances(Tree* src, Tree* par) {
if (src == NULL) {
return;
}
pr[src->val][0] = par ? par->val : -1;

for (int i = 1; i < 20; i++) {
if (pr[src->val][i - 1] != -1) {
pr[src->val][i] = pr[pr[src->val][i - 1]][i - 1];
} else {
pr[src->val][i] = -1;
}
}

ances(src->left, src);
ances(src->right, src);
}

int getAncestor(int val, int k) {
cout << val << " " << k << endl;
if (val == -1 || k == 0) {
// cout<<k<<" "<<val<<endl;
return val;
}
for (int i = 19; i >= 0; i--) {
if (k >= (1 << i)) {
return getAncestor(pr[val][i], k - (1 << i));
}
}
return -1;
}
};```
```

```                        ```Solution in Java :

import java.util.*;

/**
* public class Tree {
*   int val;
*   Tree left;
*   Tree right;
* }
*/
class AncestorQuerier {
int[][] dp;
public AncestorQuerier(Tree root) {
dp = new int[dfs(root) + 5][25];
for (int i = 0; i < dp.length; i++) Arrays.fill(dp[i], -1);
dfs(root, root);
}
int dfs(Tree tr) {
if (tr == null)
return 0;
return dfs(tr.left) + dfs(tr.right) + 1;
}
void dfs(Tree root, Tree par) {
if (root == null)
return;
int vr = root.val, vp = par.val;
if (root != par) {
dp[vr][0] = vp;
for (int i = 1; i < 24; i++) {
if (dp[vr][i - 1] != -1)
dp[vr][i] = dp[dp[vr][i - 1]][i - 1];
}
}
dfs(root.left, root);
dfs(root.right, root);
}

public int getAncestor(int val, int k) {
int cur = val;
for (int i = 0; i < 24; i++) {
if ((k | (1 << i)) == k) {
cur = dp[cur][i];
}
if (cur == -1)
return -1;
}
return cur;
}
}```
```

```                        ```Solution in Python :

class AncestorQuerier:
def __init__(self, root):
self.p = {}

def go(r, f):
if r:
a = self.p[r.val] = [-1] * 18
if f:
a[0] = f.val
for i in range(1, 18):
if a[i - 1] < 0:
break
a[i] = self.p[a[i - 1]][i - 1]
go(r.left, r)
go(r.right, r)

go(root, None)

def getAncestor(self, val, k):
if val not in self.p:
return -1
for i in range(18):
if (k >> i) & 0x01:
val = self.p[val][i]
if val < 0:
return -1
return val```
```

## 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

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 -