Παρασκευή 13 Δεκεμβρίου 2024

Interview questions for Java Backend #1

TreeSet - how does it sort incoming elements?


TreeSet accepts only objects, and sorts with their Comparable implementation


Case 1: Integer, Double, Long, etc

TreeSet uses their already existing Comparable implementation and sorts the numeric values automatically.


Case 2: Custom objects

We must implement our own Comparable for these custom objects. 




TreeSet - how does it use hashcode or equals?

No, it only uses compareTo() (or Comparator) to sort or locate an element.

HashSet uses hashcode() and equals()




TreeSet - what is a useful use case?

Hibernate gives us unordered result set.

We can feed it in a TreeSet to obtain an ordered/sorted set according to our business needs

E.g. treeSet.addAll(hibernateUnorderedSet)



TreeSet - how to traverse?

With advanced for loop or with iterator. Best use iterator - also removes items when iterating without ConcurrentModification.




Equals() - what's its purpose?

By default, Object.equals() checks equality by memory references

But in our business domain and collections, we may want to define equality with our own terms (override equals())

When we override equals, we must override hashcode, because of equals-hashcode contract:



Equals() - does HashSet use it?

HashSet and all Set collections (and Map ones) are hash-based.

That means they check for matching using hashcode() first. But in case of hashcode collision, they use equals() secondly.



Hibernate - what data structures to use per case?

Arraylist - For read-only collections, when random access is acceptable

LinkedList - For read-write collections, when modifications are frequent. LinkedList is fast for inserting/removing elements.

For example, use for Students of a Course entity, with @ElementCollection.



HashSet - When we want to filter query results with unique values


TreeSet - When we want to sort query results by our business comparison implementation.


Example for both:




TreeSet - how to have two comparison fields?

Sort answer: Using Comparator and overriding its compare()
Check: https://stackoverflow.com/questions/6850611/sort-a-list-of-objects-by-multiple-fields





Hibernate - when are entity beans loaded?



Hibernate session functions

session.get()
- Fetches immediately
session.load()
- Fetches when entity is queried (lazy)

- Note: Spring Data achieves this when Optional is used:


session.createQuery()
- Fetches immediately

session.refresh() / session.merge()
- After entity context is flushed, entities are again reloaded.


Lazy, Eager FetchType

E.g. Eager fetches child entities immediately, while Lazy only when accessed by parent.


Note: Spring Data uses Hibernate proxies for @Lazy and checked when data is actually asked, thus executing the query.


Second-level cache

We can ask Hibernate to first check in cache (e.g. Redis)



Cascade

Used on child entities, to indicate if parent events should propagate to children. Examples: - CascadeType.PERSIST
- CascadeType.REMOVE
- CascadeType.DETACH

This clearly affects entity beans loading.



COALESCE - explain?

Coalesce returns the first non-null value of a collection:


NVL- explain?
NVL provides a default value if the "salary" is null:



Query to troubleshoot a slow query?

Use EXPLAIN before your query



REST - is /person/1/delete ok?

No, DELETE person/1, is ok. The action (delete) shouldn't be included in http uri. It's already defined in our HTTP delete method.

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

What may be missing, or could get better?