Donate Us

Monday, March 9, 2020

March 09, 2020

Longest consecutive Sequence

Longest consecutive Sequence
Send Feedback

You are given with an array of integers that contain numbers in random order. Write a program to find the longest possible sequence of consecutive numbers using the numbers from given array.

You need to return the output array which contains consecutive elements. Order of elements in the output is not important.

Best solution takes O(n) time.

If two sequences are of equal length then return the sequence starting with the number whose occurrence is earlier in the array.

Input Format :
Line 1 : Integer n, Size of array
Line 2 : Array elements (separated by space)
Constraints :

1 <= n <= 50000

Sample Input 1 :
13
2 12 9 16 10 5 3 20 25 11 1 8 6 
Sample Output 1 :
8 
9 
10 
11 
12
Sample Input 2 :
7
3 7 2 1 9 8 1
Sample Output 2 :
7
8
9
Explanation: Sequence should be of consecutive numbers. Here we have 2 sequences with same length i.e. [1, 2, 3] and [7, 8, 9], but output should be [7, 8, 9] because the starting point of [7, 8, 9] comes first in input array.
Sample Input 3 :
7
15 24 23 12 19 11 16
Sample Output 3 :
15 
16

Thursday, March 5, 2020

March 05, 2020

BST Class


Implement the BST class which includes following functions -

Given an element, find if that is present in BSt or not. Return true or false.

2. insert -

Given an element, insert that element in the BST at the correct position. Assume unique elements will be given.

3. delete -

Given an element, remove that element from the BST. If the element which is to be deleted has both children, replace that with the minimum element from right sub-tree.

4. printTree (recursive) -

For printing a node with data N, you need to follow the exact format -

N:L:x,R:y

wherer, N is data of any node present in the binary tree. x and y are the values of left and right child of node N. Print the children only if it is not null.

There is no space in between.

You need to print all nodes in the recursive format in different lines.

Note : main function is given for your reference which we are using internally to test the class.





Compilar Code


/***************
 * BinaryTreeNode class already given -
 *
class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left;
BinaryTreeNode<T> right;

public BinaryTreeNode(T data) {
this.data = data;
}
}
***************/

/**************
 * Main function that we are using internally
 *
public static void main(String[] args) {
BinarySearchTree bst = new BinarySearchTree();
int choice, input;
while(true) {
choice = s.nextInt();
switch(choice) {
case 1 :
input = s.nextInt();
bst.insertData(input);
break;
case 2 :
input = s.nextInt();
bst.deleteData(input);
break;
case 3 :
input = s.nextInt();
System.out.println(bst.search(input));
break;
default :
bst.printTree();
return;
}

}
*******************/

Driver Code


public class BinarySearchTree {
    
private BinaryTreeNode<Integer> root;
    private int size;
    
    
    private static boolean isPresentHelper(BinaryTreeNode<Integer> root,int x)
    {
        if(root == null)
        {
            return false;
        }
        
        if(root.data == x)
        {
            return true;
        }
        
        if(x<root.data)
        {
            return isPresentHelper(root.left,x);
        }
        
     else
        {
            return isPresentHelper(root.right,x);
        }
    }
    
    
    public boolean search(int x)
    {
        return isPresentHelper(root,x);
    }
    
    private BinaryTreeNode<Integer> insertDataHelper(BinaryTreeNode<Integer> node,int input)
    {
        if(node == null)
        {
            BinaryTreeNode<Integer> newRoot = new BinaryTreeNode<>(input);
            
            return newRoot;
        }
        
        
        if(input<node.data)
        {
           node.left = insertDataHelper(node.left,input);
            
           
           }
        
        if(input>node.data)
        {
          node.right   = insertDataHelper(node.right,input);
            
            
           
        }
        return node;
    }
    
    //Now we will make a function to ensert data into the bst
    
    public void insertData(int input)
    {
        root = insertDataHelper(root,input);
        
    }
    
    
    private void printTreeHelper(BinaryTreeNode<Integer> node)
    {
        if(node == null)
        {
            return;
        }
        
        System.out.print(node.data+":");
        
        if(node.left != null)
        {
            System.out.print("L:"+node.left.data+",");
        }
        
        if(node.right != null)
        {
            System.out.print("R:"+node.right.data);
        }
        
        System.out.println();
        
        printTreeHelper(node.left);
        
        printTreeHelper(node.right);
    }
    
    //Now we will make a function to printTree
    public void printTree()
    {
        printTreeHelper(root);
    }
    
    //Now this functio will use to find minimum element fron right side
    
    private  int minimum(BinaryTreeNode<Integer> root)
    {
        if(root==null)
{
return Integer.MAX_VALUE;
}
int leftMin = minimum(root.left);
int rightMin = minimum(root.right);
return Math.min(root.data, Math.min(leftMin,rightMin));
}
    
    
    
    //Now we will make a function to delete data from the node
    
    private BinaryTreeNode<Integer> deleteDataHelper(BinaryTreeNode<Integer> root,int x)
    {
        if(root == null)
        {
            return null;
        }
        
        if(root.data < x)
        {
            root.right = deleteDataHelper(root.right,x);
            
            return root;
        }
        
        if(root.data>x)
        {
            root.left = deleteDataHelper(root.left,x);
            return root;
        }
        
        if(root.left==null && root.right==null)
        {
            return null;
        }
        
        if(root.left != null && root.right==null)
        {
            return root.left;
            
        }
        
        if(root.right != null && root.left==null)
        {
            return root.right;
            
        }
        
        //now if booth children is prestent
        
        int rightMin = minimum(root.right);
        
        root.data = rightMin;
        
        root.right = deleteDataHelper(root.right,rightMin);
        
        return root;
        
        
    }
    
    public void deleteData(int input)
    {
        root = deleteDataHelper(root,input);
    }
    
}


March 05, 2020

Level wise linkedlist

Given a binary tree, write code to create a separate linked list for each level. You need to return the array which contains head of each level linked list.

Input format :
Elements in level order form (separated by space). If any node does not have left or right child, take -1 in its place.
Output format : Each level linked list is printed in new line (elements separated by space).
Sample Input :
5 6 10 2 3 -1 -1 -1 -1 -1 9 -1 -1
Sample Output :
5 
6 10 
2 3 
9

This code is contribited bu Pushpendra








import java.util.ArrayList;
import java.util.*;


public class Solution {

/* Binary Tree Node class
* class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left;
BinaryTreeNode<T> right;

public BinaryTreeNode(T data) {
this.data = data;
}
}
*/

/* class Node<T> {
T data;
Node<T> next;
Node(T data){
this.data = data;
}
}
*/


Main Code That You have to Write


public static ArrayList<Node<BinaryTreeNode<Integer>>> LLForEachLevel(BinaryTreeNode<Integer> root) {
        
        Node<BinaryTreeNode<Integer>> head = null;
        Node<BinaryTreeNode<Integer>> tail = null;

        

        ArrayList<Node<BinaryTreeNode<Integer>>> arr = new  ArrayList<Node<BinaryTreeNode<Integer>>>();
        
        
        
        Queue<BinaryTreeNode<Integer>> pendingChild = new LinkedList<BinaryTreeNode<Integer>>();
        
         Queue<BinaryTreeNode<Integer>> q = new LinkedList<BinaryTreeNode<Integer>>();
        
        pendingChild.add(root);
        
        while(!pendingChild.isEmpty())
        {
            while(!pendingChild.isEmpty())
            {
                
                BinaryTreeNode<Integer> front = pendingChild.poll();
                
                Node<BinaryTreeNode<Integer>> node = new Node<BinaryTreeNode<Integer>>(front);//here we have maked a node with front data
                
                if(head == null)
                {
                    
                    head = node;
                    tail = node;
                }
                else{
                    tail.next = node;
                    
                    tail = tail.next;
                }
                
                //Now we will add left and right child of root into q queue
                
                if(front.left != null)
                {
                    q.add(front.left);
                }
                
                if(front.right != null)//this is for adding write chold of front
                {
                    q.add(front.right);
                    
                }
            }
            
            //Before going to second we will add thr head of link list into arrayList;
            
            arr.add(head);
            
            //now we reached into second line
            
            head =null;
            tail = null;
            
              Queue<BinaryTreeNode<Integer>> temp = new LinkedList<BinaryTreeNode<Integer>>();
            
            temp = pendingChild;
            
            pendingChild = q;
            
            q = temp;
            
            
        }
        
        return arr;
        
        



}


}

Wednesday, March 4, 2020

March 04, 2020
Construct BST
Send Feedback

Given a sorted integer array A of size n which contains all unique elements. You need to construct a balanced BST from this input array. Return the root of constructed BST.

Note : If array size is even, take first mid as root.
Input format :
Line 1 : Integer n (Size of array)
Line 2 : Array elements (separated by space)
Output Format :
BST elements (in pre order traversal, separated by space)
Sample Input :
7
1 2 3 4 5 6 7
Sample Output :
4 2 1 3 6 5 7 


  1. public class Solution {

  2. /* Binary Tree Node class 
  3.  * 
  4.  * class BinaryTreeNode<T> {
  5. T data;
  6. BinaryTreeNode<T> left;
  7. BinaryTreeNode<T> right;
  8. public BinaryTreeNode(T data) {
  9. this.data = data;
  10. }
  11. }
  12. */
  13.    
March 04, 2020
Elements Between K1 and K2
Send Feedback

Given a Binary Search Tree and two integers k1 and k2, find and print the elements which are in range k1 and k2 (both inclusive).

Input format :
Line 1 : Elements in level order form (separated by space)
(If any node does not have left or right child, take -1 in its place)
Line 2 : Two Integers k1 and k2
Output Format :
Required elements (separated by space)
Sample Input :
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
6 10
Sample Output :
6 7 8 10







public class Solution {

/* Binary Tree Node class
 *
 * class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left;
BinaryTreeNode<T> right;

public BinaryTreeNode(T data) {
this.data = data;
}
}
*/

public static void printNodeFromK1ToK2(BinaryTreeNode<Integer> root,int k1,int k2){
       
        if(root==null)
{
return;
}






if(root.data<k1)
{
printNodeFromK1ToK2(root.right,k1,k2);
}

else if(root.data>k2)
{
printNodeFromK1ToK2(root.left,k1,k2);

}
else
{
//System.out.print(root.data+" ");

printNodeFromK1ToK2(root.left,k1,k2);
System.out.print(root.data+" ");

printNodeFromK1ToK2(root.right,k1,k2);
//System.out.print(root.data+" ");

}

}


}

March 04, 2020
Search In BST
Send Feedback

Given a BST and an integer k. Find if the integer k is present in given BST or not. Return the node with data k if it is present, return null otherwise.

Assume that BST contains all unique elements.

Input Format :
Line 1 : Elements in level order form (separated by space)
(If any node does not have left or right child, take -1 in its place)
Line 2 : Integer k

Output Format :

Node with data k
Sample Input 1 :
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
2
Sample Output 1 :
2
Sample Input 2 :
8 5 10 2 6 -1 -1 -1 -1 -1 7 -1 -1
12
Sample Output 2 :
(empty)

public class Solution {

/* Binary Tree Node class
 * 
 * class BinaryTreeNode<T> {
  T data;
  BinaryTreeNode<T> left;
  BinaryTreeNode<T> right;
  
  public BinaryTreeNode(T data) {
   this.data = data;
  }
 }
 */
//Main code

 public static BinaryTreeNode<Integer> searchInBST(BinaryTreeNode<Integer> root , int k){
  if(root==null)
  {
   return null;
  }
  
  if(root.data==k)
  {
   return root;
  }
  
  
  if(k<root.data)
  {
   return searchInBST(root.left ,k);
   
  }
  

   return searchInBST(root.right ,k);

 }
}

Thursday, December 26, 2019

December 26, 2019

DBS bank online hiring challenge for Freshers 2020 batch

DBS Bank online hiring challenge:-
DBS Bank has announced their online hiring challenge for 2020 batch student. Students who has good command in programming language and have problem solving approach they can apply DBS bank online hiring challenge. We have given apply link below by clicking on that link they can apply online hiring challenge.



Subscribe Our channel


About DBS Bank:-

DBS bank is a Singaporean multinational financial company. DBS bank was set-up by Singaporean government in 1968.

oin our WhatsApp group




Company Name : DBS Bank
Job Location : Across India
Position : Associate
Hackathon Deadline: JAN 12, 11:59 PM

DBS Hack2Hire Eligibility Criteria :

  • Computer Science graduate(CS/IT) passing out in 2020 with 60% marks throughout your academic career.
Job Description:
  • Have a strong knowledge of distributed computing, Operating systems, cloud infrastructure, , mobile and web applications development, Have an understanding of Full Stack application development
  • Have an understanding of Full Stack application development
  • Have a passion to learn new technologies and drive major transformation
About Programme:
  • The Technology Associate Programme is designed for fresh graduates to develop their technical competencies and professional skills
  • The program envisions a holistic approach with cross functional and cross technology acumen enabling us to leverage your expertise.
  • We provide platform to innovate new ideas across projects ranging around – Customer Focus, Application development, Platform Development, System Development, Big Data and Database Management and Security and Risk management.

How To Apply DBS Hack2Hire :

Interested and eligible candidates can directly apply through tech assessment challenge on HackerRank (to be taken on a laptop).
Last Date For Assessment: 12TH January 2020, 11:59 PM (GMT +5:30)
Online Assessment Link: Click Here To Apply



Popular Posts

COO Prime Naukri

COO Prime Naukri

Total Pageviews

Tags

Popular Posts