# Virtual Boolean Array - Google Top Interview Questions

### Problem Statement :

```Implement a boolean array which implements the following methods:

BooleanArray() which initializes an array of size 2 ** 31 with all false values.

void setTrue(int i) which sets the value at index i to true.

void setFalse(int i) which sets the value at index i to false.

void setAllTrue() which sets the value at every index to true.

void setAllFalse() which sets the value at every index to false.

boolean getValue(int i) which returns the value at index i.

Constraints

0 ≤ n ≤ 100,000 where n is the number of method calls

Example 1

Input

methods = ["constructor", "getValue", "setAllTrue", "getValue", "setFalse", "getValue"]

arguments = [[], [9], [], [3], [4], [4]]`

Output

[None, False, None, True, None, False]

Explanation

a = BooleanArray()

a.getValue(9) == False

a.setAllTrue()

a.getValue(3) == True

a.setFalse(4)

a.getValue(4) == False

Example 2

Input

methods = ["constructor", "setTrue", "getValue", "setFalse", "getValue"]

arguments = [[], [5], [5], [5], [5]]`

Output

[None, None, True, None, False]

Explanation

a = BooleanArray()

a.setTrue(5)

a.getValue(5) == True

a.setFalse(5)

a.getValue(5) == False```

### Solution :

```                        ```Solution in C++ :

class BooleanArray {
public:
bool base = false;
set<int> diffs;

BooleanArray() {
base = false;
diffs.clear();
}

void setTrue(int i) {
if (base == false)
diffs.insert(i);
else
diffs.erase(i);
}

void setFalse(int i) {
if (base == true)
diffs.insert(i);
else
diffs.erase(i);
}

void setAllTrue() {
base = true;
diffs.clear();
}

void setAllFalse() {
base = false;
diffs.clear();
}

bool getValue(int i) {
return (base == true ? (diffs.count(i) > 0 ? false : true)
: (diffs.count(i) > 0 ? true : false));
}
};```
```

```                        ```Solution in Java :

import java.util.*;

class BooleanArray {
public Map<Integer, Boolean> map;
public boolean allTrue;

public BooleanArray() {
map = new HashMap<>();
}

public void setTrue(int i) {
map.put(i, true);
}

public void setFalse(int i) {
map.put(i, false);
}

public void setAllTrue() {
map.clear();
allTrue = true;
}

public void setAllFalse() {
map.clear();
allTrue = false;
}

public boolean getValue(int i) {
if (map.containsKey(i))
return map.get(i);
return allTrue;
}
}```
```

```                        ```Solution in Python :

class BooleanArray:
def __init__(self):
self.arr = {}
self.default = False

def setTrue(self, i):
self.arr[i] = True

def setFalse(self, i):
self.arr[i] = False

def setAllTrue(self):
self.arr = {}
self.default = True

def setAllFalse(self):
self.arr = {}
self.default = False

def getValue(self, i):
if i not in self.arr:
return self.default

return self.arr[i]

immortal
96

1 year ago
Even more compact memory footprint depending upon assignment.

class BooleanArray:
def __init__(self):
self.state = False
self.d = set()

def clear(self):
self.d = set()

def setTrue(self, i):
if not self.state:
elif i in self.d:
self.d.remove(i)

def setFalse(self, i):
if self.state:
elif i in self.d:
self.d.remove(i)

def setAllTrue(self):
self.clear()
self.state = True

def setAllFalse(self):
self.clear()
self.state = False

def getValue(self, i):
if i in self.d:
return not self.state
return self.state```
```

## Castle on the Grid

You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):

## Down to Zero II

You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.

## Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

## Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

## QHEAP1

This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element