MiU Technical Interview Prep
MiU Technical Interview Prep
data[0] = 0;
data[1] = 7;
data[--size] = 9;
System.out.println("The value of size is: " + size); //Output: 0
System.out.println("The content of data is: " + Arrays.toString(data));
//Output: [9, 7, 0]
}
-------------------------------------------------------
int size = 0;
for (int i = 0; i <= size; ++i) {
size--;
}
System.out.println(size);// Output: -1
---------------------------------------------
int size = 0;
for (int i = 0; i <= size; ++i) {
size++;
}
System.out.println(size);// Output: Infinite loop
----------------------------------------------
static void outputSize(){
int size =4;
for (int i=0;i<=size;i++){
size =size-2;
}
System.out.println(size);//Output: 0
}
---------------------------------------
y+=i;
}
System.out.println(y);//Output: 0+1+2=3
-----------------------------------------------
static void outputDo(){
int a=3;
do {
a=a+1;
}
while (a<5);
System.out.println(a);//Output: 5
}
---------------------------------------------------------
for (int i = 0; i < 3; i++) {
}
}
//output: 1 0, 2 0, 2 1
-----------------------------------
}
}
String s1 = "MOON";
String s2 = new String("MOON");
System.out.println(s1==s2); //Output: False;
//if you try to change the final values in java it will print the compilation
error;
}
public static void main(String[] args) {
int x = 1;
System.out.println(--x--); //Output: error;
}
// -----------------------Leader Number-----------------------
sum += a[i];
}
}
//System.out.println("Sum: "+ sum);
return sum;
}
// ------------------------Lucky Sum----------------------
Write a method called myMethod that takes in two integer arrays and
returns the index of the smallest number. The smallest number should only
be present in the first array and not in the second.
}
return index;
}
//---------------------My Array--------------------
sum += a[i];
if(sum>=a[i+1]){
return 0;
}
}
return 1;
----------------------------------OOP Problems-----------------
------------------------------Customer class------------------
Write customer class which prints name + units consumed and amount paid.
(if units consumed >= 100 amount paid = 25 if units consumed < 100 amount paid =
units consumed * 0.25)
Customer customer1 = new Customer('John Doe', 20)
use oop best practices(any no. of classes, constructors, set and get methods)
class Customer{
//Property methods
//Constractor mathods
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Customer c = new Customer("Wege");
c.detail();
}
--------------------------------Employee class-----------------------
create a java class that have three fields name, id, fee with constractors, getter,
setter and other class
setting feilds and display value and calculate total fee using methods.
---------------------------Shape
Interface-----------------------------------------------------------
EXERCISE: Create a Shape interface having methods area () and perimeter (). Create
2 subclasses,
Circle and Rectangle that implement the Shape interface. Create a class Sample with
main method and
demonstrate the area and perimeters of both the shape classes. You need to handle
the values of length, breath, and radius in respective classes to calculate their
area and perimeter.
SOLUTION:
interface Shape {
double area();
double perimeter();
}
@Override
public double area() {
@Override
public double perimeter() {
@Override
public double area() {
@Override
public double perimeter() {
}
}
---------------------------------Implementations-----------------
---------------------------------Linked List------------------
class Node{
int value;
Node next;
class LinkedList{
private Node head;
private Node tail;
private int size;
//print method
public void printll(){
Node temp = head;
while(temp!=null){
System.out.print(" "+ temp.value);
temp = temp.next;
}
}
//append method
public void append(int value){
Node node = new Node(value);
if(size == 0){
head = node;
tail = node;
}else{
tail.next = node;
tail = node;
}
size++;
}
//Remove last
while(temp.next != null){
pre = temp;
temp = temp.next;
}
tail = pre;
tail.next = null;
size--;
if(size == 0){
head = null;
tail = null;
}
return temp;
}
public void prepend(int value){
Node node = new Node(value);
if(size == 0){
head = node;
tail= node;
}else{
node.next = head;
head = node;
}
size++;
}
public Node removeFirst(){
if(size == 0) return null;
}
}
}
public void printQueue(){
Node temp = front;
while(temp!=null){
System.out.print(" < "+temp.value+" > ");
temp = temp.next;
}
System.out.println("");
}
}
--------------------------------------------------Queue Using
Array-----------------------------
}else{
rear++;
arr[rear]= value;
}
System.out.println(value+ " is added to the queue");
}
}
public int deQueue(){
if(isEmpty()){
System.out.println("The queue is empty");
return -1;
}else{
int result = arr[front];
front++;
System.out.println(result+" is removed from the queue");
if(front>rear){
front = rear = -1;
}
return result;
}
}
public int peek(){
if(!isEmpty()){
int result = arr[front];
System.out.println(result+" is peeked without removing from the queue");
return result;
}else{
return -1;
}
}
public void delete(){
arr = null;
System.out.println("The queue is deleted successfully!");
}
}
// main method
public static void main(String[] args) {
Queue queue = new Queue(5);
queue.enQueue(1);
queue.enQueue(2);
queue.enQueue(3);
queue.enQueue(4);
queue.enQueue(5);
// System.out.println(queue.deQueue());
System.out.println(queue.peek());
queue.delete();
System.out.println(queue.peek());
*/
class Solution {
public static void main(String[] args) {
class Stack{
private Node topOfStack;
private int size;
class Node {
int value;
Node next;
}else {
node.next = topOfStack;
topOfStack = node;
}
size++;
System.out.println(value+ " is pushed ");
}
public Node pop(){
if(siz == 0){
topOfStack = null;
}
System.out.println(temp.value+ " is poped");
return temp;
}
public Node peek(){
if(isEmpty()) return null;
else{
Node temp = topOfStack;
System.out.println(temp.value+" is peeked");
return temp;
}
}
public void delete(){
topOfStack = null;
System.out.println("The stack is deleted!");
}
public void printStack(){
Node temp = topOfStack;
while(temp != null){
System.out.println(temp.value);
temp = temp.next;
}
}
}
------------------------------------Tree
----------------------------------------------
// Binarysearchtree Node class;
Binary Search Tree is a node-based binary tree data structure which has the
following properties:
-The left subtree of a node contains only nodes with keys lesser than the node’s
key.
-The right subtree of a node contains only nodes with keys greater than the node’s
key.
-The left and right subtree each must also be a binary search tree.
------------------------------Array problems-----------------------------
// sumArray
int sum = 0;
for(int i=0; i<arr.length; i++){
sum += arr[i];
}
System.out.println(sum);
return sum;
}
//GridArray
int sum = 0;
for (int i=0; i<arr.length; i++){
sum += arr[i];
}
System.out.println(sum/arr.length);
return (sum/arr.length);
}
//Lenear Search
static int linearSearch1(int[] arr, int value){
}
System.out.println("Second lowest number is : " + second_min);
}
//check duplicate
------------------------------------Searching-----------------------
// Binary Search
}
---------------------------Using recursive binary search----------------
public static int BSA(int arr[], int start, int end, int value)
{
if (start<=end) {
int mid = (start + end) / 2;
if (arr[mid] == value)
return mid;
if (value < arr[mid])
return BSA(arr, start, mid - 1, value);
return BSA(arr, mid + 1, end, value);
}
return -1;
}
-----------------------------Revers an Array-----------------
return count;
}
pow *= base;
}
return pow;
}
}
System.out.println("Second lowest number is : " + second_min);
}
return arr;
}
Java HashMap
- is data structure wich store items in "key/value" pairs
e.g
Java HashSet
A HashSet is a collection of items where every item is unique
==-----------------------------------------------------
e.g
-To check whether an item exists in a HashSet, use the contains() method:
Example
cars.contains("Mazda");
Example
cars.remove("Volvo");`
Example
cars.clear();
-----------------------------------------SQL NOTE------------------------
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name
FROM table_name
WHERE condition);
SELECT *
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;
-----sql comment----
--Select all:
SELECT * FROM Customers;
-----------------------Creating table-----------------
1. What is polymorphism
1. What is polymorphism?
-Method Overloading: is when methods have the same name with different
parameter.
-Method Overriding: is re-defining of methods of super class into sub class.
-The left subtree of a node contains only nodes with values lesser than the
node’s value.
-The right subtree of a node contains only nodes with values greater than the
node’s key.
-The left and right subtree each must also be a binary search tree.
4. What is the difference between binary search tree and binary search algorithm?
-Binary Search Tree is a node-based binary tree data structure.
-Binary search algorithm is an algorithem used to search values in a sorted
arry using devide and concur method.
- Queue is a data structure that store items in FIFO (First in First out)
manner.
- Stack is a data structure that store items in LIFO(Last in First out)
manner.
----------------------------------OOP Note-------------------------------
Class:
- is a template for creating objects. or
- is a blueprint for creating objects.
Object:
-is an instance of a class.
Constractor:
-is a special method of a class that used to initialize object. or
- is a method of a class and it is called when the object is created.
Method/Function:
-is a block of code w/c only runs w/n it is called.
Method Overloading:
Method Overriding:
Abstraction: Getter and Setter are methods used to protect your data and make your
code more secure.
- is hiding internal details and showing only the required
things/information.
Encapsulation:
- is the process of grouping data in a single section
- is puting everything in a single box.
Inheritance:
-is re-using the properties of the existing class and object.
(Specialization)
- is reusing the code from previous class and adding extra features.
polymorphism:
Runtime Polymorphism:
- When the super class reference holding the object of sub class and the
overriden method is called.
- it achived by method Overriding.
- it also called Dynamic method dispatch.
- Calling method dynamically b/c the program make the decission at runtime
for w/ch object to call
Abstract Class:
- cannot create object but can be used as a reference.
- if sub class inherites from abstract class it should override all abstract
methods.
Interface:
- cannot be used to creat objects.
-interface methods have no body.
-used to achive polymorphysm
-methods are by default abstract and public.
-attributes are by default public, static, final.
-multiple inheritance can be achieved using interface.
Access Modifieres
----------------
Final:(class): cannot be inherited.
Abstract: (class): cannot be used to create object.
------------------------------Good Luck!-------------------------