## Find All Elements in an Array which appears more than N/K times, N is Array Size and k is a Number

Similarly, original Boyer-Moore Majority Vote Algorithm defined “Majority element of an array A[1 … n]: An array is said to have a majority element if more than half of its entries are the same.” In other words, the majority element is the element that appears more than ⌊ n/2 ⌋. One difference is finding more… Read More »

## Merge two stored arrays with different sizes.

1. Question. You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order. For example: int[] a = {2, 3, 4, 8, 10, 100, 0, 0, 0, 0}; int[] b = {1, 4,… Read More »

## Binary search algorithm

1. Getting. In binary search algorithm, we find an element x in a sorted array by using the principle of divide and conquer. First, we compare x to the middle point of the array. If x is less than the middle point, then we search the left of array. Otherwise, we look for the right… Read More »

## Singleton Pattern

1. Definition. Singleton Pattern makes sure that a class only have one instance and provide a global point of access to it. It also belongs to Creation Pattern Group. Singleton Pattern should be used where we only need one of: Thread pools, caches, dialog boxes, object that handle preferences and registry settings, objects used of… Read More »

## Observer Pattern

Observer Pattern defines a one-to-many relationship between a set of objects. When the state of one object changes, all of its dependents are notified/updated automatically. It also belongs to Behavior pattern group. 1. A customized notification system. Several newspaper agencies like CNN, NYTime, BBC are subscribed to a feed of new tweets and may want… Read More »

## Building a Hypermedia-Driven RESTful Web Service with Spring HATEOAS

1. Introduction. This note follows up the rest-hateoas tutorial from Spring which describes the general concept about HATEOAS. According to the Richardson Maturity Model, HATEOAS is considered the final level of REST, which explains that the REST resources contain not only data, but links to related resources. 2. Solution with Spring HATEOAS. From the last… Read More »

## Hibernate cache in theory

The note will follow: 1. Introduction. Hibernate has a two-level cache architecture: first-level cache and second-level cache. Figure 1: Hibernate’s two levels cache (source: Manning) The first-level cache is the persistence context cache (scope of Session). The cache ensures that when the application requests the same persistent object twice in a particular session, it gets back… Read More »

## Paging and sorting with Spring Data JPA and Querydsl

1. Introduction. Retrieved data with paging and storing is a basic feature of any application. This can be handled by database side (page and sort in a stored procedure, or native SQL query), and Spring Data JPA via use QueryDSL or Specifications. The note will present how to implement QueryDSL with Optional in Java 8.… Read More »

## Scaling up and scaling out in Enterprise Systems

The two most important things of an enterprise application are performance and scaling because it needs to store and retrieve as much data and as fast as possible. This note will consider scaling up and out metrics in details. Scaling up (Vertical Scaling): Add more resources to a single machine such as CPU, Memory, Disk… Read More »