DRW On-Site Flashcards

1
Q

Find all unique path’s in a binary tree

A

public List binaryTreePaths(TreeNode root) {
List answer = new ArrayList();
if (root != null) searchBT(root, “”, answer);
return answer;
}
private void searchBT(TreeNode root, String path, List answer) {
if (root.left == null && root.right == null) answer.add(path + root.val);
if (root.left != null) searchBT(root.left, path + root.val + “->”, answer);
if (root.right != null) searchBT(root.right, path + root.val + “->”, answer);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

explain tree map

A

TreeMap is sorts its keys in ascending order. It is not synchronized

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Find ways to seat a family on a plane

A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is closure?

A

A stateful function. Gives you acces to an outer function’s scope from an inner function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How are elements in a hash table stored in memory?

A

They’re stored in an array. The hash function computes the index to store the value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Sorting questions

A

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Give an integer n find how many digits 11^N are ‘1’

A
def elevenToPowerOf(n):
    # Anything to the zero is 1.
if n == 0: return "1"
    # Otherwise, n  1:
        n = n - 1
    # Make multiply by eleven easy.

    ten = num + "0"
    num = "0" + num

    # Standard primary school algorithm for adding.
        newnum = ""
        carry = 0
        for dgt in range(len(ten)-1,-1,-1):
            res = int(ten[dgt]) + int(num[dgt]) + carry
            carry = res // 10
            res = res % 10
            newnum = str(res) + newnum
        if carry == 1:
            newnum = "1" + newnum
    # Prepare for next multiplication.

    num = newnum

# There you go, 11^n as a string.

return num
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Given a tree with each node holding an integer. Find the most unique number of any path

A
// A node of binary tree
struct Node {
    int data;
    struct Node *left, *right;
};
// A utility function to create a new Binary
// Tree node
Node* newNode(int data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
int largestUinquePathUtil(Node* node, unordered_map m)
{
    if (!node)
        return m.size();
    // put this node into hash
    m[node->data]++;
int max_path = max(largestUinquePathUtil(node->left, m),
                   largestUinquePathUtil(node->right, m));
    // remove current node from path "hash"
    m[node->data]--;
// if we reached a condition where all duplicate value
// of current node is deleted
if (m[node->data] == 0)
    m.erase(node->data);

return max_path; }
// A utility function to find long unique value path
int largestUinquePath(Node* node)
{
    if (!node)
        return 0;
    // hash that store all node value
    unordered_map hash;
    // return max length unique value path
    return largestUinquePathUtil(node, hash);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Given an integer. If that number was represented in binary. How many bits would be set to ‘1’

A

Convert this number to its string binary representation then loop through. Or if you cannot convert it to one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a function that gets the number of squares between two numbers

A

static int countSquares(int a, int b)
{
int cnt = 0; // Initialize result

        // Traverse through all numbers
        for (int i = a; i <= b; i++)
        // Check if current number 'i' is perfect
        // square
        for (int j = 1; j * j <= i; j++)
            if (j * j == i)
                cnt++;
    return cnt;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Difference between hashtable and hashmap

A

Hashtable is synchronized. Better for multi threading. Does not allow null keys
HashMap is not synchronized. Better for non threaded applications. Allows one null key

How well did you know this?
1
Not at all
2
3
4
5
Perfectly