Technical blog and complete tutorial on popular company interview questions with detailed solution and Java program on Data structure, Algorithms, Time and space complexity, Core Java, Advanced Java, Design pattern, Database, Recursion, Backtracking, Binary Tree, Linked list, Stack, Queue, String, Arrays etc asked in companies like Google, Amazon, Microsoft, Facebook, Apple etc. for beginners and professionals.
Tuesday, 16 April 2019
Sunday, 14 April 2019
Simple Deadlock Program in Java
Simple java program to create Deadlock.
Deadlock describes a situation where two or more threads are blocked forever, waiting for each other.
Let's consider an example, in the office we have shared Printer and Scanner where Employees has ability to do scanning and printing.
1. John has bunch of documents that it wants to Print first and also want to take a Scan later.
(Print and Scan)
2. Michael has bunch of documents that it wants to Scan first and also want to take a Print later.
(Scan and Print)
Difference between process and thread
Difference between process and thread.
Point 1:
A process is an executing instance of an application.
Thread is independent path of execution within a process. Process can have multiple threads,
Point 2:
Threads itself is capable enough to do all the things a process does and thread is a part of process that is why thread can be termed as light-weight process.
Point 3:
Since all the Threads are part of a same Process they all share same memory allocated by Process.
Process use memory allocated by OS.
Point 4:
Inter-thread communication between threads is easy whereas Inter-process communication is difficult.
Point 5:
Processes have independent data and code segments.
Thread shares the data segment, code segment, files etc. with its peer threads.
Point 6:
Process switching is complex as compared to thread switching because of the amount of variables need to be maintained in both of the case.
Per process items | Per thread items |
---|---|
Address space | Program counter |
Global variables | Registers |
Open files | Stack |
Child processes | State |
Pending alarms | |
Signals and signal handlers | |
Accounting information |
You may also like to see
Sort Linked list using Merge sort
Bubble Sort
Heap Sort
Selection Sort
Insertion Sort
How ConcurrentHashMap works and ConcurrentHashMap interview questions
How Much Water Can A Bar Graph with different heights can Hold
Interview Questions-Answer Bank
Enjoy !!!!
If you find any issue in post or face any error while implementing, Please comment.
Sunday, 7 April 2019
Saturday, 6 April 2019
Friday, 5 April 2019
Find Moving Average of Last N numbers in a Stream.
Find Moving Average of Last N numbers in a Stream.
You are given a stream of numbers, calculate moving average of last N numbers in a stream.
In other words,
Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.
Lets see sample input and output for better understanding:
Tuesday, 2 April 2019
Search element In Sorted Rotated Array in Java
Search the element In Sorted Rotated Array in Java.
Given an array which is sorted in ascending order and is rotated, say for example
Example: original array [1,2,3,4,5,6,7] might become [3,4,5,6,7,1,2]
You are given a key to search. If key is found in the array return its index, otherwise return -1.
Note: You may assume no duplicate exists in the array, find an element in the rotated array in
O(log n) time.
Lets see sample input and output:
Monday, 1 April 2019
Find largest number in Binary Search Tree which is less than or equal to N
Largest number in Binary Search Tree which is less than or equal to N.
We have a binary search tree and a number N. Our goal is to find the greatest number in the binary search tree that is less than or equal to N. Print -1 if the the value of the element doesn't exists.
We have to find the largest number inside the binary search tree that is smaller than or equal to the target number N.
Lets see sample input and output: