Google Interview Question
Front-end Software EngineersWe can use the array itself as a Min Heap which has root as the minimum element. Start from item 1 to n of the given array and keep inserting in the min heap. During insertion if we find the item already present then we dont insert and continue with next item. This Min Heap will be created as an almost complete binary tree in-place in the given array itself.
So this will be in-place O(nlogn) approach.
The way suggested by @fitish is also in place but will require an extra O(n) scan after the sort.
Please comment if you have any.
I think this and some similar problems are related to Element Distinctness problem (EDP). One can see : "Element Discreteness Problem" in wiki.
So the algo may be:
Sort ascending array to make it partially ordered
i = 0;
if (array[i] - array[++i])
put those into output array
print out output array
Why isnt anyone using a HashSet or any Set implementation.
The algo is:
for every integer in inputarray
{
if (!integerset.add)
{
duplicate detected, scrap it
}
}
return integerset
this is main example for Set interface in java doc...
please comment on my thoughts..
Here O(n) time and O(1) space solution of the problem
comments appreciated
#include <algorithm>
#include <iostream>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define DBAR(arr,a,b) cout<<#arr<<" : ";FOR(dbar,a,b) cerr<<arr[dbar]<<" ";cerr<<endl;
/*======================================Templates Ends========================*/
/* Main Code Starts from here */
#define N 22
int main (int argc, char const* argv[])
{
int arr[N] = {1,2,2,3,3,3,4,4,5,6,7,7,7,8,8,6,10,10,11,11,23,23};
int k =0;
FOR(i,1,N-1) {
DBAR(arr,0,N-1);
if(arr[i]>arr[k]&& k+1==i) {
k++;
}
else {
while(arr[i]<=arr[k])i++;
//DB(arr[i]);
if(i<N)
swap(arr[++k],arr[i]);
}
}
DBAR(arr,0,N-1);
return 0;
}
It can be done in O(n) using hashmap, and also if we create BST by ignoring creation of nodes with duplicate nodes. I tried to program in Java, source code below:
BSTNode.java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package careercup.google;
/**
*
* @author Harit
*/
public class BSTNode {
private int value;
private int leftChild;
private int rightChild;
public BSTNode(int v){
value = v;
leftChild = -1;
rightChild = -1;
}
public int getValue(){
return this.value;
}
public int getChild(boolean left){
if(left){
return this.leftChild;
}
else{
return this.rightChild;
}
}
public void setChild(int child, boolean left){
if(left){
this.leftChild = child;
}else{
this.rightChild = child;
}
}
}
RemDupArrayBST:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package careercup.google;
import java.util.ArrayList;
/**
*
* @author Harit
*/
public class RemDupArrayBST {
/* Output Array of Nodes */
private static ArrayList<BSTNode> nodes = new ArrayList<BSTNode>();
private static int parent = -1;
private static boolean left = false;
public static void main(String args[]){
/* Sample Input Array */
//int[] input_array = new int[]{3,1,2,1};
//int[] input_array = new int[]{3,5,4,2,1,2,3,1};
int[] input_array = new int[]{1,2,3,2,4,4};
/* end sample input */
for(int i=0; i<input_array.length; i++){
if(nodes.size() == 0){
/* tree is empty, add root */
BSTNode node = new BSTNode(input_array[i]);
nodes.add(node);
}else{
/* search if node already exists */
if(!contains(input_array[i])){
BSTNode node = new BSTNode(input_array[i]);
nodes.add(node);
nodes.get(parent).setChild(nodes.size()-1, left);
}else{
System.out.println(input_array[i] + " : already exists - ignoring");
}
}
}
/* final print */
System.out.print("Inorder Traversal: ");
inOrderTraverse(nodes.get(0));
System.out.println("");
System.out.print("PostOrder Traversal: ");
postOrderTraverse(nodes.get(0));
System.out.println("");
System.out.print("PreOrder Traversal: ");
preOrderTraverse(nodes.get(0));
System.out.println("");
}
private static boolean contains(int value){
int index = 0;
while(index >= 0){
if (nodes.get(index).getValue() == value){
return true;
}
if (nodes.get(index).getValue() > value){
parent = index;
left = true;
index = nodes.get(index).getChild(true);
}else{
parent = index;
left = false;
index = nodes.get(index).getChild(false);
}
}
return false;
}
private static void inOrderTraverse(BSTNode node){
if(node == null){
return;
}
if(node.getChild(true) != -1){
inOrderTraverse(nodes.get(node.getChild(true)));
}
System.out.print(node.getValue() + " ");
if(node.getChild(false) != -1){
inOrderTraverse(nodes.get(node.getChild(false)));
}
}
private static void postOrderTraverse(BSTNode node){
if(node == null){
return;
}
if(node.getChild(true) != -1){
postOrderTraverse(nodes.get(node.getChild(true)));
}
if(node.getChild(false) != -1){
postOrderTraverse(nodes.get(node.getChild(false)));
}
System.out.print(node.getValue() + " ");
}
private static void preOrderTraverse(BSTNode node){
if(node == null){
return;
}
System.out.print(node.getValue() + " ");
if(node.getChild(true) != -1){
preOrderTraverse(nodes.get(node.getChild(true)));
}
if(node.getChild(false) != -1){
preOrderTraverse(nodes.get(node.getChild(false)));
}
}
}
Remeber, they want you to write all your code on the whiteboard... I think you might need several whiteboards for all that!
I mean that during the interviews Google requires you to write complete and valid source code on the whiteboard. The above solution, while it probably works, is much too large for that. Probably a good indicator that there is a much simpler solution, which there is and others have posted.
Array in question is already seems sorted.
int i=1;
int j=0;
while(i<n)
{
while(a[i++] == a[j]);
a[j++] = a[i++];
}
a[0..j] will have required elements.
Simple Javascript solution
var a = [1,2,4,6,3,4,4,3,2,2,7,3,6,8];
var removeDup = function(){
for(i=0;i<a.length;){//IMPORTANT LINE
if(a[i-1] ==a[i]){
a.splice(i,1);
}
else{
i++;
}
}
return a;
}
document.writeln(removeDup());
var res = [];
var len = arr.length;
// Start timing now
for(var i = 0; i < len; i++) {
if(res.indexOf(arr[i]) == -1) {
res.push(arr[i])
}
}
This has a running time of O(n) which is not bad..
JS solution (makes sense for a Frontend Position)
- Aditya October 02, 2010