Antra Interview Experience
Antra Interview Experience
Part I First Interview
1 | import java.io.*; |
Part II Source from Web
multithread
1) What is multithreading?
Multithreading is a process of executing multiple threads simultaneously. Multithreading is used to obtain the multitasking. It consumes less memory and gives the fast and efficient performance. Its main advantages are:
- Threads share the same address space.
- The thread is lightweight.
- The cost of communication between the processes is low.
2) What is the thread?
A thread is a lightweight subprocess. It is a separate path of execution because each thread runs in a different stack frame. A process may contain multiple threads. Threads share the process resources, but still, they execute independently.
hash table vs hash map
1. Synchronization or Thread Safe : This is the most important difference between two . HashMap is non synchronized and not thread safe.On the other hand, Hashtable is thread safe and synchronized.
2. Null keys and null values : HashMap allows one null key and any number of null values, while Hashtable do not allow null keys and null values in the Hashtable object.
3. Iterating the values: HashMap object values are iterated by using iterator .Hashtable is the only class other than vector which uses enumerator to iterate the values of Hashtable object.
4. Fail-fast iterator : The iterator in HashMap is fail-fast iterator while the enumerator for Hashtable is not.
5. Performance : HashMap is much faster and uses less memory than Hashtable as former is unsynchronized . Unsynchronized objects are often much better in performance in compare to synchronized object like Hashtable in single threaded environment.
volatile
volatile keyword.
volatile keyword indicates that a variable’s value will be modified by different threads.
When a java variable is declared volatile,
- The value of that varilable will not be cached thread-locally, all read/write will be directed to the “main memory”.
- Access to the variable is synchronized on itself.
Difference between synchronized and volatile:
synchronized/ˈsiNGkrəˌnīz/ | volatile/ˈvälədl/ |
---|---|
Synchronized block applies only to object. | volatile keyword can be applied to Object or primitive types. |
Synchronized block does not work on null. Attempting to synchronize on a null object will throw a NullPointerException. | volatile variables can be null. |
synchronized blocks other threads while one thread is accessing the block. | volatile does not block any threads. |
synchronization occurs when you enter a synchronized block. | synchronization occurs when a volatile variable is accessed. |
final
The final keyword can be used with a class, method, and variables.
- If it is used with class then it prevents inheritance by not allowing you to create subclasses.
- If it is used with methods then it prevents overriding, you cannot override a final method in Java.
- If it is used with variables then they are treated as constant because you cannot change their value once assigned.
- The final variable can only be assigned during the class instantiate cycl
checked exception
SQL normalization
Database Normalization is a set of rules that are applied to a database, such that the schema of the database ensures that all the rules are being followed. These rules are also known as Normal Forms and are widely used while designing database solutions.
First Normal Form (1NF)
- Data is stored in tables with rows that can be uniquely identified by a Primary Key.
- Data within each table is stored in individual columns in its most reduced form.
- There are no repeating groups.
Second Normal Form (2NF)
- All the rules from 1NF must be satisfied.
- Only those data that relates to a table’s primary key is stored in each table.
Third Normal Form (3NF)
- All the rules from 2NF must be satisfied.
- There should be no intra-table dependencies between the columns in each table.
Part III Coding Source
Find top k freq element in a list
Part IV Source from HR
Usage of Collections in coding:
List
List in Java provides the facility to maintain the ordered collection. It is a interface.
The List interface is found in the java.util
package and inherits the Collection interface.
1 |
|
In short, you can create the List of any type. The ArrayList<T>
and LinkedList<T>
classes are used to specify the type. Here, T
denotes the type.
Example:
1 | import java.util.*; |
1 | //How to convert Array to List |
1 | import java.util.*; |
LinkedList
1 | import java.util.*; |
ArrayList | LinkedList |
---|---|
1) ArrayList internally uses a dynamic array to store the elements. | LinkedList internally uses a doubly linked list to store the elements. |
2) Manipulation with ArrayList is slow because it internally uses an array. If any element is removed from the array, all the bits are shifted in memory. | Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory. |
3) An ArrayList class can act as a list only because it implements List only. | LinkedList class can act as a list and queue both because it implements List and Deque interfaces. |
4) ArrayList is better for storing and accessing data. | LinkedList is better for manipulating data. |
Set
1 | import java.util.*; |
Map
HashMap (hashCode equals)
Sorting a List/Array in a given attribute. Like sort a list of students by age or name.
two objects, which are not equal to equals() method can still return same hashCode.
Hashcode is a fundamental method in every Java.Lang.Object. The hashCode method is an inbuilt method that returns the integer hashed value of the input value. Here are a few key concepts to remember:
Multiple invocations of the hashCode must return the same integer value within the execution of a program unless the Object used within the equals method changes.
The integer value need not be the same for the next implementation. If two or more objects are equal according to the equals method, then their hashes should be equal too. If two or more objects are not equal according to the equals method, then their hashes can be equal or unequal.
basic java coding:
Primitive Data Types
https://www.interviewgrid.com/interview_questions/java/java_data_types
String
immutable
Strings in Java programming language are immutable, i.e. once a string object is created its value cannot be changed. When you change the value of a string reference variable, internally the java virtual machine creates a new string in memory and returns that value. The old string still exists in memory but is not being referenced.
String constant pool
Strings literals in Java programming language are immutable. Once a string is created in memory it cannot be changed. If a string literal has to be changed, the Java virtual machine creates the new string literal in memory and returns it.
To make it more memory efficient, the Java virtual machine has a special area of memory called the String constant pool. When a new string is required, the Java virtual machine first checks if the string literal exists in the string constant pool. If it exists then the string reference variable will refer to this string literal, a new literal in not created in memory. If the string literal does not exists in memory then a new string literal is created in the string constant pool.
String-builder String-Buffer
String objects are immutable. Once a string object is created then its value cannot change. Every time you want to get a modified string value, the Java virtual machine will create a new string object. So if you modify a string 100 times, 100 string objects are created in memory.
Unlike string objects, StringBuilder objects are mutable. You can change the value of a string builder object without creating a new object. So you can modify a StringBuilder object many times, but only a single instance of the StringBuilder object is created.
StringBuilder is not thread safe, i.e it’s methods are not synchronized; whereas StringBuffer is thread safe, i.e it’s methods are synchronized. Since the methods of StringBuilder are not synchronized, it is faster than StringBuffer. If thread safety is not a requirement, you should use StringBuilder instead of StringBuffer.
key methods in String class
toCharArray() - Returns an array of the string’s characters
equals() - Returns true if this string matches the text of another string object.
equalsIgnoreCase() - Returns true if this string matches the text of another string object ignoring the case.
length() - Returns the length of the string.
subString() - Returns part of the string. You have to specify the start index and end index.
1 | str = str.substring(1); |
concat() - Appends another string to this string.
1 | String str = "123"; |
split() - Splits the string based on a delimiter. returns a string array
1 | String str = "geekss@for@geekss"; |
key methods in String-builder
append() - Appends another string to this string.
insert() - Inserts another string into this string. You have to specify the index at which the string has to be included.
delete() - Deletes part of the string.
reverse() - Reverses the characters of a string.
toString() - Returns a string representation of the StringBuffer or StringBuilder.
char Array
Declaring Char Array
Declaration of a char array can be done by using square brackets:
1 | char[] JavaCharArray; |
The square brackets can be placed at the end as well.
1 | char JavaCharArray[]; |
The next step is to initialize these arrays
Initializing Char Array
A char array can be initialized by conferring to it a default size.
1 | char[] JavaCharArray = new char[4]; |
This assigns to it an instance with size 4.
We use the following code to assign values to our char array:
1 | char[] JavaCharArray = new char[5]; |
Loops play a very important role when it comes to avoiding repetitive code, this is the next bit of this Char Array in Java article,
1 | char[] JavaCharArray = {'r', 's', 't', 'u', 'v'}; |
Array
1 | int intArray[]; //declaring array |
sub-class
Abstract Class and Interface
Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces.
abstract classes
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
It can not be instantiated by keyword “new“. The normal class can be instantiated by keyword “new“.
It would need constructors since it requires the memory allocation for the members or properties.
Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
An abstract class can have both abstract and regular methods:
1 | // Abstract class |
interfaces
Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.
- An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods.
- An interface cannot be instantiate.
- An interface does not contain any constructors.
- An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
- An interface is not extended by a class; it is implemented by a class.
- An interface can extend multiple interfaces.
interface
It is used to achieve total abstraction.
Since java does not support multiple inheritance in case of class, but by using interface it can achieve multiple inheritance .
It is also used to achieve loose coupling.
Interfaces are used to implement abstraction. So the question arises why use interfaces when we have abstract classes?
constructor
getter/setter
Leetcode easy level
Advanced Concepts in Java:
OOP
SOLID
Principle | Description |
---|---|
Single Responsibility Principle | Each class should be responsible for a single part or functionality of the system. |
Open-Closed Principle | Software components should be open for extension, but not for modification. |
Liskov Substitution Principle | Objects of a superclass should be replaceable with objects of its subclasses without breaking the system. |
Interface Segregation Principle | No client should be forced to depend on methods that it does not use. |
Dependency Inversion Principle | High-level modules should not depend on low-level modules, both should depend on abstractions. |
https://www.educative.io/edpresso/what-are-the-solid-principles-in-java
MVC
design patterns
multithreading
OOP
There are four main OOP concepts in Java. These are:
- Abstraction. Abstraction means using simple things to represent complexity. We all know how to turn the TV on, but we don’t need to know how it works in order to enjoy it. In Java, abstraction means simple things like objects, classes, and variables represent more complex underlying code and data. This is important because it lets avoid repeating the same work multiple times.
- Encapsulation. en·cap·su·la·tion. This is the practice of keeping fields within a class private, then providing access to them via public methods. It’s a protective barrier that keeps the data and code safe within the class itself. This way, we can re-use objects like code components or variables without allowing open access to the data system-wide.
- Inheritance. /inˈherədəns/This is a special feature of Object Oriented Programming in Java. It lets programmers create new classes that share some of the attributes of existing classes. This lets us build on previous work without reinventing the wheel.
- Polymorphism. /ˌpälēˈmôrfizəm/This Java OOP concept lets programmers use the same word to mean different things in different contexts. One form of polymorphism in Java is method overloading. That’s when different meanings are implied by the code itself. The other form is method overriding. That’s when the different meanings are implied by the values of the supplied variables. See more on this below.
basic network:
HTTP
TCP
basic DB:
SQL
joins
index
any thing showing on their resumes.
- Collections in Java.
- Multithreading in Java.
- Lambda and Stream in Java 8.
- HTTP protocol.
- Have Any one of : AWS, MongoDB, Angular, React, NodeJS, Algorithms