Google Interview Question for Software Engineers

Country: United States

Comment hidden because of low score. Click to expand.
1
of 1 vote

This is one of the great example for topological sorting.

1 -->3 -->5

1-->3 -->9

9-->5

After topological sorting 1->3->9->5

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````vector<int> GetOverallArray(const vector<vector<int>>& A)
{
vector<int> result;
multimap<int, pair<vector<int>::const_iterator, vector<int>::const_iterator>> itr_tail;

for (const auto& arr : A)
{
itr_tail.emplace(arr.front(), make_pair(arr.cbegin(), arr.cend()));
}

while (true)
{
bool not_process = false;
auto prev_it = itr_tail.begin();
if (itr_tail.size() <= 1)
break;
if (prev_it != itr_tail.end())
{
int cur_val = prev_it->first;
int count = 0;
for (auto it = next(prev_it); it != itr_tail.end(); ++it)
{
if (cur_val == it->first)
{
not_process = true;
count++;
auto next_it = next(prev_it->second.first);
if (next_it != prev_it->second.second)
{
itr_tail.emplace(*next_it, make_pair(next_it, prev_it->second.second));
}
itr_tail.erase(prev_it);
}
else
{
if (count > 0)
{
auto next_it = next(prev_it->second.first);
if (next_it != prev_it->second.second)
{
itr_tail.emplace(*next_it, make_pair(next_it, prev_it->second.second));
}
itr_tail.erase(prev_it);

result.emplace_back(cur_val);
}
count = 0;
cur_val = it->first;
}

prev_it = it;
}

if (count > 0)
{
auto next_it = next(prev_it->second.first);
if (next_it != prev_it->second.second)
{
itr_tail.emplace(*next_it, make_pair(next_it, prev_it->second.second));
}
itr_tail.erase(prev_it);
result.emplace_back(cur_val);
}

if (!not_process)
{
prev_it = itr_tail.begin();
if (prev_it != itr_tail.end())
{
auto next_it = next(prev_it->second.first);
if (next_it != prev_it->second.second)
{
itr_tail.emplace(*next_it, make_pair(next_it, prev_it->second.second));
}
itr_tail.erase(prev_it);
}
}
}

}

return result;
}

int main()
{
vector<vector<int>> A;
A.emplace_back(vector<int>{1,3,5});
A.emplace_back(vector<int>{1, 3, 9});
A.emplace_back(vector<int>{9, 5});

vector<int> ret = GetOverallArray(A);

return 0;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````int[] array = ...
Integer[] boxedArray = IntStream.of(array).boxed().toArray(Integer[]::new);
Set<Integer> set = IntStream.of(array).boxed().collect(Collectors.toSet());
//or if you need a HashSet specifically
HashSet<Integer> hashset = IntStream.of(array).boxed()
.collect(Collectors.toCollection(HashSet::new));``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

``````package T15;

import java.util.ArrayList;

public class T15 {

private static ArrayList<ArrayList<Integer>> input = new ArrayList<>();
private static ArrayList<Integer> result = new ArrayList<>();

public static void main(String[] args) {

init();
process();

System.out.println(result);
}

private static void process() {

ArrayList<Integer> getInner1 = new ArrayList<>();
ArrayList<Integer> getInner2 = new ArrayList<>();

for (int i = 0; i < input.size(); i++) {
getInner1 = input.get(i);
for (int j = i + 1; j < input.size(); j++) {
getInner2 = input.get(j);

compare(getInner1, getInner2);
}
}
}

private static void compare(ArrayList<Integer> g1, ArrayList<Integer> g2) {
for (int t : g1) {
compare2(t, g2);
}
}

private static void compare2(int t, ArrayList<Integer> g) {
for (int i = 0; i < g.size(); i++) {
if (t == g.get(i))
}
}

private static void init() {
ArrayList<Integer> inner = new ArrayList<>();

inner = new ArrayList<>();

inner = new ArrayList<>();

}

}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

The problem should be:
"given a bunch of arrays having all unique elements. Find out the shortest common super sequence of em."

This can be solved using topological sorting.

If we don't include the shortest common super sequence then the answer to this will simply be the concatenation of the arrays. If elements are not unique then the answer ll be closely related to LCS of n strings, which is very inefficient.

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.