Category: java

  • Variables – Java vs Javascript

    Variables are critical to any programming language. It is where you store temporary data. Not all data needs to be stored in the database, whatever doesn’t need to be stored in the database and is needed temporarily during your program execution are stored in variables. Variables in Javascript differ from variables in Java in quite…

  • Java vs Javascript – Say Hello World!

    One of the complaints about Java programming language is that it is verbose. Imagine you are new to programming. And you want to learn to program. The first thing most of us traditionally do is to print “Hello World!” . And to do this in Java you need to go through a lot of ordeal:…

  • try catch block in Java vs Javascript

    In both Java and JavaScript, the try-catch block is used to handle exceptions that may occur in the code. The basic structure of a try-catch block in both languages is similar: a block of code is put in a “try” block, and any exceptions that are thrown in that block are caught and handled in…

  • Delete Node in a Linked List given only the node

    Problem: You are given a node to delete from a linked list. But you are not given the head node. Delete it in O(1) time and O(1) space complexity. Constraints: For example, Given the linked list : 1 -> 4 -> 7 -> 8 And the node 7 to be deleted The output should be…

  • Odd Even Linked List

    Problem: Given the head of a linked list , reorder the linked list so that the elements in odd indices are grouped together followed by the elements in event indices. The first index is assumed to be 1 (odd) and the second index even and so on. For example, For the below input: 1 ->…

  • Find first and last position of element in sorted array

    Problem: Given a sorted integer array find the first and last position of given target element For example, If given array is [ 1,3,6,6,7,9] and target is 6 Then output is [2,3] which represents the first and last index position of the element 6 For the same input if the target is 8 Then the…

  • How to implement Trie Data Structure?

    Trie is a datastructure useful to store set of words. It is a tree with a lot of children , one for each letter of the alphabets. So if you want to store lower case English letter words each node can have unto 26 child nodes. Every word is represented by as many nodes as…

  • Find the Lowest Common Ancestor of two given nodes of a Binary Search Tree

    Given two nodes and the root node of a binary search tree, Find the lowest common ancestor of those two nodes. A common ancestor node is a node who is the parent of both the given nodes. Lowest common ancestor means there is no other common ancestor node which is lower than this node in…

  • Kth Smallest Element in a Binary Search Tree

    Given a binary search tree, find the kth smallest element in it. For example, For the below tree: If k = 3, Then the kth (3rd) smallest element is 3. If k = 2, Then the output is 2. Try out the solution here: https://leetcode.com/problems/kth-smallest-element-in-a-bst/ Solution: Hint : Use inorder traversal. Explanation: Approach 1 –…

  • Construct Binary Tree From Preorder and Inorder Traversals

    Given the preorder and inorder traversal arrays of a tree, construct it. For example, Given preorder = [3,9,20,15,7], inorder = [9,3,15,20,7] You need to construct the below tree: you need to return the root node of the above tree in your code. Try it here: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ Solution: Hint: Using root node from preorder traversal ,…