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 = ...

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, ...

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 / \ ...

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) ...

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

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

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) + ...

Title: Detect Cycle in a Directed Graph Source: www.geeksforgeeks.org Given a directed graph, check whether the graph contains a cycle or not. Your function should return true if the given graph contains at least one cycle, else return false. For example, the following graph contains three cycles 0->2->0, 0->1->2->0 and 3->3, so your function must ...

Title: Check if a given number is Fancy Source: www.geeksforgeeks.org A fancy number is one which when rotated 180 degrees is the same. Given a number, find whether it is fancy or not. 180 degree rotations of 6, 9, 1, 0 and 8 are 9, 6, 1, 0 and 8 respectively Examples: Input: num = ...

Title: How to design a tiny URL or URL shortener? Source: www.geeksforgeeks.org How to design a system that takes big URLs like “http://www.geeksforgeeks.org/count-sum-of-digits-in-numbers-from-1-to-n/” and converts them into a short 6 character URL. It is given that URLs are stored in database and every URL has an associated integer id. One important thing to note is, ...