## N-ary Tree PreOrder Traversal

Title: N-ary Tree Preorder Traversal Source: leetcode.com Given an n-ary tree, return the preorder traversal of its nodes’ values. For example, given a 3-ary tree: Return its preorder traversal as: [1,3,5,6,2,4]. Note: Recursive solution is trivial, could you do it iteratively? You could find another explanation/ problem related to Preorder Traversal here. Java solution (Recursive) ...

## Maximum Product of Word Lengths

Title: Maximum Product of Word Lengths Source: leetcode.com Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Example 1: Input: [“abcw”,”baz”,”foo”,”bar”,”xtfn”,”abcdef”] Output: ...

## Convert JSON to Java POJO

Here’s a brief tutorial to covert JSON to JAVA POJOs (code) or in other words, generating Java POJOs from a given JSON definition. This could prove really handy in the Microservices world, where you may have to work with and generate a lot of request response objects whether it be internal or third party API ...

## Power of Three

Title: Power of Three Source: leetcode.com Given an integer, write a function to determine if it is a power of three. Follow up: Could you do it without using any loop / recursion? Java solution Java /*https://leetcode.com/problems/power-of-three/*/ public class PowerOfThree { public boolean isPowerOfThree(int n) { if(n==0) return false; while(n!=1) { //System.out.println(n); int remainder = ...

## Decode Ways2

Title: Decode Ways Source: leetcode.com A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 1234 'A' -> 1'B' -> 2...'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, ...

## Verify Preorder Serialization of a Binary Tree4

Title: Verify Preorder Serialization of a Binary Tree Source: leetcode.com One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node’s value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ ...

## Implement Reverse DNS Look Up Cache

Title: How to Implement Reverse DNS Look Up Cache? Source: www.geeksforgeeks.org Reverse DNS look up is using an internet IP address to find a domain name. For example, if you type 74.125.200.106 in browser, it automatically redirects to google.in. How to implement Reverse DNS Look Up cache? Following are the operations needed from cache. 1) ...

## Knapsack Problem1

Title: Dynamic Programming | 0-1 Knapsack Problem Source: www.geeksforgeeks.org Given weights and values of n items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. In other words, given two integer arrays val[0..n-1] and wt[0..n-1] which represent values and weights associated with n items respectively. Also ...

## Remove duplicates from an unsorted linked list

Title: Remove duplicates from an unsorted linked list Source: www.geeksforgeeks.org Write a removeDuplicates() function which takes a list and deletes any duplicate nodes from the list. The list is not sorted. For example if the linked list is 12->11->12->21->41->43->21 then removeDuplicates() should convert the list to 12->11->21->41->43. METHOD 1 (Using two loops) This is the ...

## Count number of ways to cover a distance

Title: Count number of ways to cover a distance Source: www.geeksforgeeks.org Given a distance ‘dist, count total number of ways to cover the distance with 1, 2 and 3 steps. Java solution Java /* http://www.geeksforgeeks.org/count-number-of-ways-to-cover-a-distance/ */ class CountWaysToCoverDistance { public int count(int dist) { if(dist==0) return 1; if(dist < 0) return 0; return count(dist-1) + ...