Showing posts with label LinkedIn. Show all posts
Showing posts with label LinkedIn. Show all posts

Thursday, September 23, 2021

[LinkedIn] Shortest Word Distance

Problem: Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.

Example:

Input: words = ["practice","makes","perfect","coding","makes"], word1 = "coding", word2 = "practice"
Output: 3
Explanation: IndexOf("coding") - IndexOf("practice") = 3 - 0 = 3
Input: words = ["practice","makes","perfect","coding","makes"], word1 = "makes", word2 = "coding"
Output: 1
Explanation: There are two places where "makes" is present in the array which is 1 and 4. The index of "coding" is 3 so the differences are 3 - 1 = 2 and 4 - 3 = 1. The minimum of theses distances (2, 1) is 1.

Constraints:

  • word1 is not equal to word2
  • word1 and word2 both are present in the given words array.


Approach: It's a straight forward problem to solve. You can look at the implementation to understand the approach.


Implementation in Java:

    public int shortestDistance(String[] words, String word1, String word2) 

    {

        int indexOfWord1 = -1;

        int indexOfWord2 = -1;

        int minDistance = Integer.MAX_VALUE;

        for (int i = 0; i < words.length; ++i) {

            if (words[i].equals(word1))

            {

                indexOfWord1 = i;

                if (indexOfWord2 != -1) {

                    minDistance = Math.min(minDistance, Math.abs(indexOfWord2 - indexOfWord1));

                }

            }

            if (words[i].equals(word2)) {

                indexOfWord2 = i;

                if (indexOfWord1 != -1) {

                    minDistance = Math.min(minDistance, Math.abs(indexOfWord2 - indexOfWord1));

                }

            }

        }

        return minDistance;

    }


Complexity: O(n)

Tuesday, March 2, 2021

[LinkedIn Question][LeetCode] Second Minimum Node In a Tournament Tree

Problem: Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. More formally, the property root.val = min(root.left.val, root.right.val) always holds.

Given such a binary tree, you need to output the second minimum value in the set made of all the nodes' value in the whole tree.

If no such second minimum value exists, output -1 instead.

Example:

Input: root = [2,2,5,null,null,5,7]
Output: 5
Explanation: The smallest value is 2, the second smallest value is 5.

Input: root = [2,2,2]
Output: -1
Explanation: The smallest value is 2, but there isn't any second smallest value.


Approach: We can use Pre-Order traversal to solve this problem.


Implementation in C#:

public int FindSecondMinimumValue(TreeNode root) 

{

        if (root == null)

        {

            return -1;

        }

        if (root.left == null)

        {

            return -1;

        }

        long secondMin = long.MaxValue;

        this.FindSecondMinimumValue(root, root.val, ref secondMin);

        return secondMin == long.MaxValue ? - 1 : (int)secondMin;

    }

    

    private void FindSecondMinimumValue(TreeNode node, int firstMin, ref long secondMin)

    {

        if (node != null)

        {

            if (node.val > firstMin && node.val < secondMin)

            {

                secondMin = node.val;

            }

            else if (node.val == firstMin)

            {

                this.FindSecondMinimumValue(node.left, firstMin, ref secondMin);

                this.FindSecondMinimumValue(node.right, firstMin, ref secondMin);

            }

        }

    }


Complexity: O(n)