Posts

Showing posts with the label Data Structures

All advanced sorting techniques using Java 8 and Streams

package com.dpq.interview.Q; import java.math.BigInteger; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; public class SortingDemo { public static void main(String[] args) throws ParseException { allSortingTechniquesAfterJava8(); } public static void allSortingTechniquesAfterJava8() throws ParseException { List<Employee> list = populateList(); System.out.println(“####################################### Natiral soring by Employee Id ###############################################”); list.stream().sorted().forEach(e -> System.out.println(e)); System.out.println(“####################################### Natiral soring by Employee Id but in desending order ###############################################”); list.stream().sorted(Collections.reverseOrder()).forEach(e -> System.out.println(e)); List<Employee> sortedList = list

Everything about Binary Tree and its all traversal techniques (recursive and itterative) with examples

package org.dpq.ds.tree;import java.util.Stack;public class Tree<T>{ public static void main(String[] args) { TreeNode<Integer> root = new TreeNode<Integer>(1); root.setLeft(new TreeNode<Integer>(2)); root.setRight(new TreeNode<Integer>(3)); root.getLeft().setLeft(new TreeNode<Integer>(4)); root.getLeft().setRight(new TreeNode<Integer>(5)); root.getRight().setLeft(new TreeNode<Integer>(6)); root.getRight().setRight(new TreeNode<Integer>(7)); Tree<Integer> tree = new Tree<Integer>(); //Tree// 1// / \// 2 3// /\ /\// 4 5 6 7 //expected result for inorder(LNR) 4 2 5 1 6 3 7 //expected result for preorder(NLR) 1 2 4 5 3 6 7 //expected result for preorder(NLR) 4 5 2 6 7 3 1 System.out.println("recursive inorder \n"); tree.inOrder(root); System.out.println("recursive pre

Spark interview questions complete guide

Image
1. Can you tell me what is Apache Spark about? Apache Spark is an open-source framework engine that is known for its speed, easy-to-use nature in the field of big data processing and analysis. It also has built-in modules for graph processing, machine learning, streaming, SQL, etc. The spark execution engine supports in-memory computation and cyclic data flow and it can run either on cluster mode or standalone mode and can access diverse data sources like HBase, HDFS, Cassandra, etc. 2. What are the features of Apache Spark? High Processing Speed : Apache Spark helps in the achievement of a very high processing speed of data by reducing read-write operations to disk. The speed is almost 100x faster while performing in-memory computation and 10x faster while performing disk computation. Dynamic Nature : Spark provides 80 high-level operators which help in the easy development of parallel applications. In-Memory Computation : The in-memory computation feature of Spark due to its DAG exec

Why pop operation in Stack using Array has flaw?

if take an example of implementation of data structure Stack using array there are some obvious flaw. Let’s take the  POP  operation of the stack. The algorithm would go something like this. Check for the stack underflow Decrement the top by 1 So there what we are doing is that, the pointer to the topmost element is decremented means we are just bounding our view actually that element stays there talking up of the memory space. If you have any primitive datatype then it might be ok but the object of an array would take a lot of memory.

Complexities of all Data Structures

Complexity for all basic Data Structure Data Structure Name         Operation       Worst Case complexity.       Average Case Complexity.      Best Case Complexity Array                                        insertion.                        O(n).                                           O(n)                                                O(n) Array                                        deletion                           O(n).                                           O(n)                                                O(n) Array                                        access                              O(1)                                             O(1)                                                 O(1) Array                                        search                             O(n).                                            O(n)                                                O(n)

Program for n’th node from the end of a Linked List

Image
Given a Linked List and a number n, write a function that returns the value at the n’th node from the end of the Linked List. For example, if the input is below list and n = 3, then output is “B” Way 1 (Use length of linked list) 1) Calculate the length of Linked List. Let the length be len. 2) Print the (len – n + 1)th node from the beginning of the Linked List. // Simple Java program to find n'th node from end of linked list class LinkedList {      Node head; // head of the list      /* Linked List node */      class Node {          int data;          Node next;          Node( int d)          {              data = d;              next = null ;          }      }      /* Function to get the nth node from the last of a         linked list */      void printNthFromLast( int n)      {          int len = 0 ;          Node temp = head;          // 1) count the number of nodes in Linked List          while (temp != null ) {              temp = temp.next;              len++;      

Implement Stack using Queues

Image
We are given a Queue data structure that supports standard operations like enqueue() and dequeue(). We need to implement a Stack data structure using only instances of Queue and queue operations allowed on the instances A stack can be implemented using two queues. Let stack to be implemented be ‘s’ and queues used to implement be ‘q1’ and ‘q2’. Stack ‘s’ can be implemented in two ways: Method 1 (By making push operation costly) This method makes sure that newly entered element is always at the front of ‘q1’, so that pop operation just dequeues from ‘q1’. ‘q2’ is used to put every new element at front of ‘q1’. push(s, x)  operation’s step are described below: Enqueue x to q2 One by one dequeue everything from q1 and enqueue to q2. Swap the names of q1 and q2 pop(s)  operation’s function are described below: Dequeue an item from q1 and return it. Below is the implementation of the above approach: /* Java Program to implement a stack using  two queue */ import java.util.*;   class GfG {