Random interview preparation subjects

JPA Repository: What It Is Used For and Main Features
What is JPA Repository?
JpaRepository
is a part of Spring Data JPA, which provides a convenient way to interact with a relational database using JPA (Java Persistence API). It extends PagingAndSortingRepository
and CrudRepository
, offering additional functionality.
Main Features of JpaRepository
CRUD Operations – Provides built-in methods like
save()
,findById()
,findAll()
,deleteById()
, etc.Pagination and Sorting – Supports
Pageable
andSort
for efficient query handling.Custom Query Methods – Supports method naming conventions like
findByName()
,findByEmailContaining()
, etc.@Query Annotation Support – Allows custom JPQL and native SQL queries.
Transaction Management – Works with Spring’s transactional support.
Lazy and Eager Fetching – Can control data fetching strategies.
Integration with Spring Boot – Works seamlessly with Spring Boot’s
@EnableJpaRepositories
.
Example Usage
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByLastName(String lastName);
@Query("SELECT u FROM User u WHERE u.email = :email")
User findByEmail(@Param("email") String email);
}
Branching Strategy in Git
Choosing a Git branching strategy depends on the project's needs. Below are some commonly used strategies:
1. Git Flow (Vincent Driessen Model)
Best for: Large projects with scheduled releases.
main
(stable production code).develop
(integration branch).feature/*
(for new features).release/*
(for preparing new releases).hotfix/*
(for urgent fixes in production).
Example:
git checkout -b feature/new-feature develop
git merge develop
2. GitHub Flow
Best for: Continuous Deployment and smaller projects.
Only
main
branch is used.Developers create feature branches and merge them via Pull Requests (PRs).
Example:
git checkout -b feature-branch
git commit -m "New feature"
git push origin feature-branch
3. Trunk-Based Development
Best for: Fast-paced CI/CD environments.
Developers work directly on
main
with short-lived feature branches.Encourages frequent merges to avoid long-lived branches.
4. GitLab Flow
Best for: Projects with multiple environments (staging, production).
Uses environment-based branches:
main
,pre-production
,production
.Developers merge into
main
, which gets tested before being pushed toproduction
.
Which One to Use?
For large teams with scheduled releases: Use Git Flow.
For CI/CD and fast releases: Use GitHub Flow or Trunk-Based Development.
For environment-specific releases: Use GitLab Flow.
Difference Between ArrayList
and LinkedList
in Java
Both ArrayList
and LinkedList
implement the List
interface, but they have different underlying data structures and performance characteristics.
Feature | ArrayList 🟢 | LinkedList 🔵 |
Underlying Structure | Dynamic array | Doubly linked list |
Access Time (get(index)) | O(1) – Direct access via index | O(n) – Traverses from head/tail |
Insertion (add at end) | O(1) amortized – If resizing needed, O(n) | O(1) – Just updates pointers |
Insertion (add in middle) | O(n) – Needs shifting elements | O(1) if at head/tail, O(n) otherwise |
Deletion (remove(index)) | O(n) – Needs shifting elements | O(1) if at head/tail, O(n) otherwise |
Memory Usage | Less (only stores elements) | More (stores elements + pointers) |
Iteration Speed | Faster (cache-friendly, continuous memory) | Slower (cache-unfriendly, scattered memory) |
When to Use Which?
✅ Use ArrayList
when:
✔️ You need fast random access (get(index)
).
✔️ Insertions and deletions happen mostly at the end.
✔️ Memory usage should be minimal.
✔️ You need a cache-friendly structure (sequential memory).
✅ Use LinkedList
when:
✔️ You need fast insertions or deletions at the beginning or middle.
✔️ Frequent insertions/removals, and you want to avoid array shifting.
✔️ You don’t need random access often (get(index)
).
Example Code
Using ArrayList
(Better for fast access)
List<Integer> arrayList = new ArrayList<>();
arrayList.add(1);
arrayList.add(2);
arrayList.get(1); // O(1) - Fast access
Using LinkedList
(Better for frequent insertions/removals)
List<Integer> linkedList = new LinkedList<>();
linkedList.add(1);
linkedList.addFirst(0); // O(1) - Fast insertion at head
linkedList.remove(1); // O(1) - Fast deletion from head
Subscribe to my newsletter
Read articles from Mihai Popescu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
