0%

Bloom Filter

What’s Bloom Filter

A Bloom filter is a data structure that’s used to test whether an element is a member of a set.
The primary advantage of a Bloom filter over other data structures is that it’s incredibly space-efficient
when dealing with large data sets.

However, this comes with a trade-off: while Bloom filters can definitively state if an element is not in the set,
it can only probabilistically determine if an element is in the set.
This means it has a certain rate of false positives.

In a Bloom filter, data is passed through several hash functions, with each function mapping the input to a
position within a bit array.

When an element is added to the Bloom filter, bits at the hashed positions are set to 1.
When testing for the presence of an element, the filter checks whether the bits at the hashed positions are set.
If they are, the filter returns “possibly in set”. If not, it returns “definitely not in set”.

Simple Java Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.BitSet;

public class BloomFilter {
private static final int DEFAULT_SIZE = 2 << 24;
private final BitSet bitSet = new BitSet(DEFAULT_SIZE);
private static final int[] seeds = new int[]{7, 11, 13, 31, 37, 61};
private SimpleHash[] func = new SimpleHash[seeds.length];

public BloomFilter() {
for (int i = 0; i < seeds.length; i++) {
func[i] = new SimpleHash(DEFAULT_SIZE, seeds[i]);
}
}

public void add(String value) {
for (SimpleHash f : func) {
bitSet.set(f.hash(value), true);
}
}

public boolean contains(String value) {
if (value == null) {
return false;
}
boolean ret = true;
for (SimpleHash f : func) {
ret = ret && bitSet.get(f.hash(value));
}
return ret;
}

public static class SimpleHash {
private int cap;
private int seed;

public SimpleHash(int cap, int seed) {
this.cap = cap;
this.seed = seed;
}

public int hash(String value) {
int result = 0;
int len = value.length();
for (int i = 0; i < len; i++) {
result = seed * result + value.charAt(i);
}
return (cap - 1) & result;
}
}
}

Unit tests

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class BloomFilterTest {
private BloomFilter bloomFilter;

@Before
public void setup() {
bloomFilter = new BloomFilter();
}

@Test
public void testAddAndContains() {
String testValue = "test";
bloomFilter.add(testValue);

Assert.assertTrue(bloomFilter.contains(testValue));
}

@Test
public void testDoesntContain() {
String testValue = "test";
String otherValue = "other";

bloomFilter.add(testValue);

Assert.assertFalse(bloomFilter.contains(otherValue));
}

@Test
public void testEmpty() {
String testValue = "test";

Assert.assertFalse(bloomFilter.contains(testValue));
}
}

348.Design Tic-Tac-Toe

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Assume the following rules are for the tic-tac-toe game on an n x n board between two players:

A move is guaranteed to be valid and is placed on an empty block.
Once a winning condition is reached, no more moves are allowed.
A player who succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game.
Implement the TicTacToe class:

- TicTacToe(int n) Initializes the object the size of the board n.
- int move(int row, int col, int player) Indicates that the player with id player plays at the cell (row, col)
of the board. The move is guaranteed to be a valid move, and the two players alternate in making moves. Return
- 0 if there is no winner after the move,
- 1 if player 1 is the winner after the move, or
- 2 if player 2 is the winner after the move.

Difficulty : Medium

Read more »

881. Boats to Save People

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
You are given an array people where people[i] is the weight of the ith person, and an infinite number of boats where each boat can carry a maximum weight of limit. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit.

Return the minimum number of boats to carry every given person.



Example 1:

Input: people = [1,2], limit = 3
Output: 1
Explanation: 1 boat (1, 2)
Example 2:

Input: people = [3,2,2,1], limit = 3
Output: 3
Explanation: 3 boats (1, 2), (2) and (3)
Example 3:

Input: people = [3,5,3,4], limit = 5
Output: 4
Explanation: 4 boats (3), (3), (4), (5)


Constraints:

1 <= people.length <= 5 * 104
1 <= people[i] <= limit <= 3 * 104

Difficulty : Medium

Read more »

How to convert int[] to Set< Integer> in Java

To Set< Integer> before JDK8

1
2
3
4
5
6
7
public static Set<Integer> toSet(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
return set;
}

To Set< Integer> After JDK8

1
2
3
public static Set<Integer> toSet(int[] nums) {
return Arrays.stream(nums).boxed().collect(Collectors.toSet());
}

To TreeSet< Integer>

1
2
3
public static TreeSet<Integer> toSet(int[] nums) {
return Arrays.stream(nums).boxed().collect(Collectors.toCollection(TreeSet::new));
}

To ArrayList< Integer>

1
2
3
public static List<Integer> toList(int[] nums) {
return Arrays.stream(nums).boxed().collect(Collectors.toList());
}

2605. Form Smallest Number From Two Digit Arrays

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Given two arrays of unique digits nums1 and nums2, return the smallest number that contains at least one digit from each array.


Example 1:

Input: nums1 = [4,1,3], nums2 = [5,7]
Output: 15
Explanation: The number 15 contains the digit 1 from nums1 and the digit 5 from nums2. It can be proven that 15 is the smallest number we can have.
Example 2:

Input: nums1 = [3,5,2,6], nums2 = [3,1,7]
Output: 3
Explanation: The number 3 contains the digit 3 which exists in both arrays.


Constraints:

1 <= nums1.length, nums2.length <= 9
1 <= nums1[i], nums2[i] <= 9
All digits in each array are unique.

Difficulty : Easy

Read more »