Grid Traveler or Unique Paths

The weather is acting somewhat erratic. Today the forecasted high in the Twin Cities of Minneapolis and St. Paul will be 20F lower than yesterday. Tomorrow it should be 10F lower than today and should be raining. The water level in pond in my backyard is very low. Some rain should help. I noticed that a couple of cranes have made their home for summer the pond. Not sure if they are the same that return every year or they are an offspring of the ones from last year. I do not know much about cranes.

Since the weather will be nice today; my wife and I will go for a walk after the end of the workday. Not sure what we will do tomorrow. Perhaps we will go to the Mall of America.

On a different note, as you might know, I work in 2-hour blocks. After each block I go up and get something to drink. A few minutes later I head back to work. A few weeks ago I moved a stationary bike from a utility room and set it up in the lower level living room. I started biking for 5 minutes after completing a 2-hour block. That is working very well. I get between 20 to 25 minutes biking each workday. Since the bike is indoors I can do it year round. Continue reading “Grid Traveler or Unique Paths”

Movies on Flight

Good day! It is a Friday in the month of May. The forecast called for rain and thunderstorms. We only received some rain. It is good to have some rain because things were starting to dry up. When vegetation dries up it may catch on fire.

In this post I will attempt to solve two flavors of the same problem. I believe one can create a few more problems by changing just a few words in the requirement. This will become clear as we read the requirements and describe the differences.

I found this problem named Movies on Flight in the LeetCode web site. Apparently Amazon has been using different versions on some of their technical assessments. Continue reading “Movies on Flight”

Letter Combinations of a Phone Number

Today is somewhat colder than yesterday in the Twin Cities of Minneapolis and St. Paul. It seems that we weekend will be quite a bit warmer than the rest of the week. This Sunday is Easter. It should be a nice day if you celebrate the holiday.

Earlier my wife went to get her hair done. Last week she got it cut for the first time since the COVID-19 pandemic started. It is amazing how the past year or so have gone by. Continue reading “Letter Combinations of a Phone Number”

Largest Triple Products – Revisited

!!! UPDATE UPDATE UPDATE !!!

I received a comment indicating an issue with the largestValues() function. The issue has been corrected. The code has been updated. Thanks for noting the issue.

Good morning! It is 04:15 AM and it is a relatively warm day. Last evening before going to bed I decided to hopefully get up a little earlier than usual. I have a permanent alarm on my phone for 06:00 AM. I use it as a fail-safe in case I do not naturally wake up before. Today I wanted to get this post done and take a look at a test I left running overnight. My wife has a doctor’s appointment early today and it is a 45 minute drive from home. She has to fast so I will follow suit. We should be leaving home around 06:45 AM.

On a separate note, I visited the ProtonMail web site. As we all know there are companies that offer free email services. They have redefined what the word free means. The cost is quite heavy regarding your privacy. In the past couple weeks I have been reading about email services. My wife and I are contemplating on opening accounts on a service in the near future. When that happens, you will see a new email address at the bottom of the posts. Continue reading “Largest Triple Products – Revisited”

Verifying an Alien Dictionary

Running tests and verifying code is the work order for the day. I have a Command Line Interface (CLI) for the storage server sending the equivalent of ping requests but at the application level. Since I started the test at around 09:30 AM today it has processed so far 1,190,833 requests and associated responses without a single warning or error entry in the log files. I will stop the test at the end of the work day.

Earlier today I was going to work on a different LeetCode problem, but when I did a search on “alien” two problems were selected. Since one was flagged Easy and the second Hard, I thought some items might apply from the first one to the second problem. Hopefully it would not take too long to solve the Easy problem so I could tackle the Hard after the end of the workday. Boy was I mistaken. Continue reading “Verifying an Alien Dictionary”

Can you solve it? Java Solution

Good morning. Hope you are doing well. It is a dark, foggy and cold morning in the Twin Cities of Minneapolis and St. Paul. I woke up around 05:00 AM. I finished reading the article Does Facebook Use Sensitive Data for Advertising Purposes? article in the Communications of the ACM magazine. I though the article was interesting. Apparently Facebook has a set of several thousand categories of which up to 1000 can be applied to each user. Not sure if there is an opt out setting. The authors of the paper suggested a tool that can be used in a couple browsers to check and delete the advertising labels assigned to your account. Continue reading “Can you solve it? Java Solution”

Design Tic-Tac-Toe in Java

It is about quitting time for today Friday January 15, 2021. Due to the snow storm the cleaning lady called to postpone for today. She will be here Monday morning. I guess different cities received different amounts of snow. I am not sure how much fresh snow fell during this storm. My guess is that we received between 2 to 3 inches. That said; the service that cleans our driveway and paths was here around 01:00 PM.

For lunch my wife prepared a Chinese meal with products from Trader Joe’s. It was quite good taking into consideration that it came from a super market. It took my wife about 30 minutes to prepare lunch from start to finish. Continue reading “Design Tic-Tac-Toe in Java”

Number of Visible Nodes

!!! UPDATE – UPDATE – UPDATE !!!

I have been asked several questions on this post. Given that you are not able to submit your solution and be tested against dozens of test cases in order to determine [1] if you have properly understood the problem or [2] generated an acceptable solution to the properly understood problem.

Since there is no interviewer at hand that you can ask questions to clarify the problem, please read the requirements and decide what the requirements are.

There is a binary tree with N nodes.
You are viewing the tree from its left side and can see only the leftmost nodes at each level. 
Return the number of visible nodes.

Note: You can see only the leftmost nodes, but that doesn't mean they have to be left nodes. 
The leftmost node at a level could be a right node.

Input
The root node of a tree, where the number of nodes is between 1 and 1000, 
and the value of each node is between 0 and 1,000,000,000

An int representing the number of visible nodes.

MY INTERPRETATION is that when I view the tree from the left side I will be looking at the first visible node in each level. The node may be a right or a left child. The note in the description seems to corroborate my thoughts.

MY APPROACH is to perform a DFS traversal. The node we can see from the left will be the first non-null node at each level. In addition, we could get the depth of the tree and it should provide the SAME answer given that no matter which side we look from (left or right) the same number of nodes should be visible.

3,null,5,7,null
main <<< arr: [3, null, 5, 7, null]
main <<< bfs:
3
null 5
7 null
main <<< visibleNodes: 3
main <<<        depth: 3
main <<< bfs (no null nodes):
3
5
7
main <<<        level: 3


3,2,5,1,4
main <<< arr: [3, 2, 5, 1, 4]
main <<< bfs:
3
2 5
1 4 null null
main <<< visibleNodes: 3
main <<<        depth: 3
main <<< bfs (no null nodes):
3
2 5
1 4
main <<<        level: 3


8
3 10
1 6 null 14
null null 4 7 null null
main <<< visibleNodes: 4
main <<<        depth: 4
main <<< bfs (no null nodes):
8
3 10
1 6 14
4 7
main <<<        level: 4


1,2,3,null,5,null,7
main <<< arr: [1, 2, 3, null, 5, null, 7]
main <<< bfs:
1
2 3
null 5 null 7
main <<< visibleNodes: 3
main <<<        depth: 3
main <<< bfs (no null nodes):
1
2 3
5 7
main <<<        level: 3


5
4 8
11 null 17 4
7 null null null 5 null
main <<< visibleNodes: 4
main <<<        depth: 4
main <<< bfs (no null nodes):
5
4 8
11 17 4
7 5
main <<<        level: 4


5
4 8
11 null 17 4
7 null null null 5 null
main <<< visibleNodes: 4
main <<<        depth: 4
main <<< bfs (no null nodes):
5
4 8
11 17 4
7 5
main <<<        level: 4


5,4,8,11,null,17,4,7,null,null,null,5,null,null,null,20,30
main <<< arr: [5, 4, 8, 11, null, 17, 4, 7, null, null, null, 5, null, null, null, 20, 30]       
main <<< bfs:
5
4 8
11 null 17 4
7 null null null 5 null
null null 20 30
main <<< visibleNodes: 5
main <<<        depth: 5
main <<< bfs (no null nodes):
5
4 8
11 17 4
7 5
20 30
main <<<        level: 5


5,4,8,11,null,17,4,7,null,null,null,5,null,null,null,20,30,100,101,102,103
main <<< arr: [5, 4, 8, 11, null, 17, 4, 7, null, null, null, 5, null, null, null, 20, 30, 100, 101, 102, 103]
main <<< bfs:
5
4 8
11 null 17 4
7 null null null 5 null
null null 20 30
100 101 102 103
main <<< visibleNodes: 6
main <<<        depth: 6
main <<< bfs (no null nodes):
5
4 8
11 17 4
7 5
20 30
100 101 102 103
main <<<        level: 6


1,2,3,4,5,6,7,null,null,null,null,null,null,null,8,null,9,10,null
main <<< arr: [1, 2, 3, 4, 5, 6, 7, null, null, null, null, null, null, null, 8, null, 9, 10, null]
main <<< bfs:
1
2 3
4 5 6 7
null null null null null null null 8
null 9
10 null
main <<< visibleNodes: 6
main <<<        depth: 6
main <<< bfs (no null nodes):
1
2 3
4 5 6 7
8
9
10
main <<<        level: 6


8,3,10,1,6,null,14,null,null,4,7,13
main <<< arr: [8, 3, 10, 1, 6, null, 14, null, null, 4, 7, 13]
main <<< bfs:
8
3 10
1 6 null 14
null null 4 7 13 null
main <<< visibleNodes: 4
main <<<        depth: 4
main <<< bfs (no null nodes):
8
3 10
1 6 14
4 7 13
main <<<        level: 4


8,3,10,1,6,null,14,null,null,4,7,13,15
main <<< arr: [8, 3, 10, 1, 6, null, 14, null, null, 4, 7, 13, 15]
main <<< bfs:
8
3 10
1 6 null 14
null null 4 7 13 15
main <<< visibleNodes: 4
main <<<        depth: 4
main <<< bfs (no null nodes):
8
3 10
1 6 14
4 7 13 15
main <<<        level: 4


10,null,15,null,5
main <<< arr: [10, null, 15, null, 5]
main <<< bfs:
10
null 15
null 5
main <<< visibleNodes: 3
main <<<        depth: 3
main <<< bfs (no null nodes):
10 
15
5
main <<<        level: 3

The test cases contain tree values. The first is the one returned by performing a BFS tree traversal and counting the number of levels. The second is a method that returns the depth of the tree. The third pass is a BFS traversal so we can count the nodes we see from the right or left in the binary tree. The null values are no longer displayed because they do not exist as far as nodes in a binary tree. Unless I am missing something; all values should be the same.

    /**
     * Test scaffolding.
     * 
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        
        // **** open buffered stream ****
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

        // **** read and split input line ****
        String[] strArr = input.readLine().split(",");

        // **** close buffered reader ****
        input.close();

        // **** create and populate integer array (may contain null) ****
        Integer[] arr = new Integer[strArr.length];
        for (int i = 0; i < strArr.length; i++) {
            if (strArr[i].equals("null"))
                arr[i] = null;
            else
                arr[i] = Integer.parseInt(strArr[i]);
        }

        // ???? ????
        System.out.println("main <<< arr: " + Arrays.toString(arr));

        // **** create and populate binary tree ****
        TreeNode root = populateTree(arr);

        // ???? ????
        System.out.println("main <<< bfs:");
        System.out.print(bfsTraversal(root));

        // **** compute and display number of visible node ****
        System.out.println("main <<< visibleNodes: " + visibleNodes(root));

        // **** find and display the depth of the BT ****
        System.out.println("main <<<        depth: " + maxDepth(root));

        // **** find and display level of BT ****
        System.out.println("main <<< bfs (no null nodes): ");
        System.out.println("main <<<        level: " + levelOrderTraversal(root));
    }

This is the test case. I believe this is the same as in the original post but with an additional computation.

    /**
     * Count visible nodes in binary tree.
     * Recursive call.
     * O(n)
     */
    static int visibleNodes(TreeNode root) {

        // **** base condition ****
        if (root == null) return 0;

        // **** recursive call(s) ****
        return Math.max(visibleNodes(root.left) + 1, visibleNodes(root.right) + 1);
    }

This is the same code as in the original post. It returns the number of visible nodes including some of the null values which indicate the place of a missing node.

    /**
     * Given a binary tree, find its maximum depth.
     * Recursive function O(log(n)).
     */
    static int maxDepth(TreeNode root) {
        
        // **** base condition ****
        if (root == null)
            return 0;

        // **** initialize the BT max depth ****
        int depth = 0;

        // **** visit left node ****
        depth = Math.max(depth, maxDepth(root.left) + 1);

        // **** visit right node ****
        depth = Math.max(depth, maxDepth(root.right) + 1);

        // **** return the max depth ****
        return depth;
    }

Compute the max depth of a binary tree.

    /**
     * Display the nodes in a binary tree per level.
     * This function returns the number of levels in the
     * specified binary tree.
     */
    static int levelOrderTraversal(TreeNode root) {

        // **** ****
        Queue<TreeNode> q = new LinkedList<TreeNode>();
        Queue<TreeNode> t = new LinkedList<TreeNode>();
        int level = 0;

        // **** prime q ****
        q.add(root);

        // **** loop while q is not empty ****
        while (!q.isEmpty()) {
 
            // **** remove head node ****
            TreeNode tempNode = q.poll();

            // **** display the value of this node ****
            System.out.print(tempNode.val + " ");

            // **** enqueue left child ****
            if (tempNode.left != null)
                t.add(tempNode.left);
 
            // **** enqueue right child ****
            if (tempNode.right != null)
                t.add(tempNode.right);

            // **** check if q is empty (end of level) ****
            if (q.isEmpty()) {

                // **** end the current level ****
                System.out.println();

                // **** increment level ****
                level++;

                // **** swap queues ****
                q = t;
                t = new LinkedList<TreeNode>();
            }
        }

        // **** return binary tree level ****
        return level;
    }

This method performs a BFS depth traversal of the tree. It displays the values of existing nodes. The placeholders for null nodes are not displayed. In addition the function counts the levels in the tree. When done the number of levels are returned. This value should match the other two values generated by the previous functions.

Looking at the output of this last function you can see that when looking at the tree from the left or from the right the number of levels should be the same. What may change is the value of the nodes when looked from the left or right sides. Note that the requirements do not make reference to the actual values of the nodes; we just care for the count.

In addition you may draw the circumferences representing the nodes in a 2D space. If you look at the binary tree from the left of the right you should just see the end lines of the end nodes which represent the diameter of the nodes. At that time disregard the links connecting nodes and count the number of nodes you see from either side. They should be the same and match all three results per test case.

Hope this is of help!

!!! NOW BACK TO THE ORIGIONAL POST!!!

Good day! Hope you are doing well. Apparently the number of COVID-19 cases in the Twin Cities of Minneapolis and St. Paul has risen. In addition it seems that the number of free ICU beds had dropped closely to 0. This implies that a person who falls sick and needs intensive care might have a tough time finding an available bed. For our own sake, please follow distancing rules and keep safe. We all will benefit from such behavior.

I have been looking at some problems in a Facebook portal. They are intended as exercises for technical interviews. Continue reading “Number of Visible Nodes”

Validate Binary Search Tree – Revisited

Hope you enjoyed Thanksgiving Day 2020. My wife and I did even though it was just the two of use for late lunch. We prepared our usual menu with some minor twists. The menu was a young turkey (we usually cook a 20+ lbs bird) which came in at around 11 lbs. with potatoes casserole and meat stuffing (we not prepare the popular / traditional bread based kind). For desert we had monkey bread which we got from one of our neighbors. We had a little more than we should, so we decided to skip our after lunch espresso.

For the turkey, we added some spices, a little garlic clove, some butter and apple vinegar. We inserted a thermometer and cook it to 175F. We then took it out of the oven and let it rest. Typically the temperature rises north of 180F. Since the turkey weighted 11 lbs., the temperature only went up to 178F. Continue reading “Validate Binary Search Tree – Revisited”

Continuous Subarray Sum

Tomorrow is Thanksgiving Day 2020. My wife finished preparing three containers with cheesy potatoes casserole. She has been making this dish as far as I can remember. Due to the ingredients we tend to have it once or twice a day. I will provide the recipe and some pictures on tomorrows post. If I am not able to solve a problem and generate a post, will generate a post as soon as possible.

We are still in the midst of the COVID-19 pandemic. About two million people around the world have die of complications related to the novel coronavirus. My wife and I have some friends’ originally from Vietnam. We learned today that one of them is in the hospital and the other barely making it at home. One of my wife’s brothers has been diagnosed with COVID-19. He, his wife and two kids are in quarantine at home. Our best wishes and our thoughts go out to them for a quick and safe recovery. Continue reading “Continuous Subarray Sum”