Epic Systems Interview Question for Software Engineer / Developers






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

forgot to add we cant use Hash :)

- cunmoad September 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If it's just an array in the question and the task is to extract unique elements then, isn't the solution as easy as:

i = a[0]
print i
foreach j in a[1..n]
{
if j != i
{
print j; i = j;
}
}

- preppin September 20, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what is the data structure of the list of array? If it is an linked list, it will be easily done in O(n) in space.

- hunt September 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its an link list do u mind posting code

- cunomad September 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

pseudo code:

if (list.size() < 1) return list;
t1 = list.head();
t2 = t1.next();
while (t2 != null) {
if (t2.key() != t1.key()) {
it1 = it2;
}
it2 = it2.next();
}
it1.next() = null;

- hunt September 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hunt, your code does not seem to do the job. You're just moving the pointers forward. There are two options: either you create a new list or trim the existing list so that at the end it just contains the unique elements. Your code is not doing either.
My approach would be two trim the existing list and in this case you have to swap elements so that you skip the repeating elements.
My take:

// Remove duplicates from a sorted list 
void RemoveDuplicates(struct node* head) 
{ 
        struct node* current = head; 
	if (current == NULL) return; // do nothing if the list is empty 
	// Compare current node with next node 
	while(current->next!=NULL) { 
		if (current->data == current->next->data) { 
			struct node* nextNext = current->next->next; 
			free(current->next); 
			current->next = nextNext; 
		} else { 
			current = current->next; // only advance if no deletion 
		} 
	} 
}

- googler September 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think googler's approach is correct

- someone September 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

All we have to do in this case is: while printing out the elements, see if it'snot equivalent to its previous element, else dont print it. Thats it!!

- Anonymous September 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can anyone tell me an efficient approach using Arrays

- Anon September 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Cunomad, wer r u getting these q's from. r these from the main test or the 2nd round. plz tell us

- Anon September 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i = a[0]
print i
foreach j in a[1..n]
{
if j != i { print j; i = j; }
}

- preppin September 20, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

c++ code

void getUnique(list<int>& input) {
  //less than 2 element
  if (input.size() < 2)
    return;

  list<int>::iterator it = input.begin();
  list<int>::const_iterator cit = input.begin();
  ++it;
  while (it != input.end()) {
    if (*it != *cit) {
      ++it;
      ++cit;
    } else {
       list<int>::iterator itTemp = it;
       ++it;
       input.erase(itTemp);
    }
  }
}

- Roger October 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

c++ code

void getUnique(list<int>& input) {
  //less than 2 element
  if (input.size() < 2)
    return;

  list<int>::iterator it = input.begin();
  list<int>::const_iterator cit = input.begin();
  ++it;
  while (it != input.end()) {
    if (*it != *cit) {
      ++it;
      ++cit;
    } else {
       list<int>::iterator itTemp = it;
       ++it;
       input.erase(itTemp);
    }
  }
}

- Roger October 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void getUnique(list<int>& input) {
  //less than 2 element
  if (input.size() < 2)
    return;

  list<int>::iterator it = input.begin();
  list<int>::const_iterator cit = input.begin();
  ++it;
  while (it != input.end()) {
    if (*it != *cit) {
      ++it;
      ++cit;
    } else {
       list<int>::iterator itTemp = it;
       ++it;
       input.erase(itTemp);
    }
  }
}

- Anonymous October 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n) solution :

for(i = 0 ; i < arraySize ; i++)
{
if(i!= arraySize && a[i] != a[i+1])
printf("%d",a[i]);
}
printf("%d",a[i-1]);

- akshaymhetre1 November 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(n) solution :

Some changes in the above code
for(i = 0 ; i < arraySize ; i++)
{
if(i!= arraySize-1 && a[i] != a[i+1])
printf("%d",a[i]);
}
printf("%d",a[i-1]);

- akshaymhetre1 November 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

void main()
{
	int a[20], n, ele;
	int x=0, cur=0, nxt = 1;
	char ans;

    do
	{
		cout << "\n Enter a no. and y/n if you would like to continue ? ";
		cin >> a[x++] >> ans;
	
	}while(ans=='y' && x<20);

	while(cur<x)
	{
		ele=a[cur];
		if(ele == a[nxt])
		{
		  while(ele==a[++nxt]);		  
		  cur=nxt; 
		}
		else
		{
		  cur++;		  
		}
		nxt++;
		cout << "\n " << ele;
	}

    cout <<"\n ";
}

- Britney February 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
	int a[10] = {1,1,1,1,1,9,5,5,5,5};
	cout<<a[0];
	for (int i=1;i<9 ;i++ )
	{		
		if (a[i] ^ a[i+1])
			cout<<a[i+1];				
	}
}

I thought bit operations are faster.

- Jay February 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Jay's solution really looks nice. Nice job.

Though, we may not worry about bitwise operations much because most compilers are smart enough to perform these optimizations anyway, they're not always as readable, and they're not always correct for negative numbers.

- Britney February 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If allowed to use c++ STL or c# genric Collections
one could use HashSet or STL Set

c#
string[]UniqueArray = new HashSet<string>(DupArray).ToArray();
Linq
var Unique = (from s in DupArray
select s).Distinct();
Python
Unique =[]
Unique.append(i) for i in DupList if not Unique.count(i)]

- kalmari.sudhakar September 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The code by Jay will fail if the input is of this form :- 1,1,2,3,3,3,4,5,5,6 .
And also why does jay , print the first element arr[0], even though that is not unique

- abh February 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This can solve the problem by Jay's code (in JAVA)

public class unique_sorted_array
{
public static void main(String args[])
{
int arr[] = {1,1,2,3,3,3,4,5,5,6};
//System.out.println(arr[0]);
for(int i=0;i<arr.length-2;i++)
{
if((arr[i] ^ arr[i+1]) == 1)
System.out.println(arr[i]);
}
int k =arr.length-1;
if((arr[k] ^ arr[k-1]) != 0)
{
System.out.println(arr[k]);
}
}

}

- abh February 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey11391" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
int a[] = {1,1,3,3,3,3,5,5,5,8,8,8,8,8};
int j = 0;

for (int i = 0; i < a.length()-1; i++) {
j = i+1;
while (a[i] == a[j]) {
j++;
}
System.out.println(""+a[i]);
i = j - 1;
} //end for

if (a[a.length()-1] != a[a.length()-2])
System.out.println(""+a[a.length()-1]);
}
}

</pre><pre title="CodeMonkey11391" input="yes">
</pre>

- Anonymous July 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printUnique(int[] arr){
if(arr.length==0 )
return;
int currentElement=arr[0];
System.out.println(arr[0]);
for(int i=1;i<arr.length;i++){
if((arr[i]^currentElement)!=0){
System.out.println(arr[i]);
currentElement=arr[i];
}

}
}

- DG November 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static List<int> RemoveDup(List<int> input)
        {
            List<int> result = new List<int>();

            for (int i = 0; i <= input.Count-1; i++)
            {
                if (!result.Contains(input[i]))
                {
                    result.Add(input[i]);
                }
            }
            return result;

}

- Teva February 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
public class uniqueValues {
	static int[] arr={1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9}; 
	public static void main(String[] args) {
		StringBuffer sb=new StringBuffer();
	    ArrayList<String> al=new ArrayList<String>();
		for(int i:arr){
			sb.append(i);
			String s=Integer.toString(i);
			al.add(s);
		}
		System.out.println(al);
		String result=al.get(0);
		for(int i=0;i<al.size();i++){
			if(!result.contains(al.get(i))){
				result+=al.get(i);
			}
		}
		System.out.println(result);
	}

}

- disun March 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using treeset:

TreeSet<Integer> values = new TreeSet<Integer>(Arrays.asList(1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9));
        
  for(Integer i : values) 
     System.out.println(i);

- Anonymous March 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I tried it this way : Please let me know if it is good enough:

package epic.java;

public class UniqueElementsInArray {

	public static void main(String[] args){
		Integer[] intArray={1,1,3,3,3,5,5,5,9,9,9, 9,10};
		int len=intArray.length;

		int i=0;
		int j=1;

		while(j<len){

			if(intArray[i]!=intArray[j]){
				intArray[i+1]=intArray[j];
				i++;					
			}
			j++;
		}
		for(int m=0;m<i+1;m++)
			System.out.println(intArray[m]);
	}

}

- Akki November 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What's wrong with everybody? Seriously? O(n) is not efficient enough!

- ydyyfshsh November 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pythonic Solution

a = [1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9]
print list(set(a))

- Devesh November 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry. Did not see that hashing wasn't allowed.

We can use the following simple looping construct.

e = a[0]
print e
for i in xrange(1,len(a)):
    if(a[i]!=e):
        e = a[i]
        print a[i]

- Devesh November 25, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RemoveDuplicates {
public static int[] remove(int[] a){
	int i=0;
	int l=a.length;
	int count=0;
	int duplicate=a[0];
	for(i=1;i<l;i++){
		if(a[i]==duplicate)
			count++;
		a[i-count]=a[i];
		duplicate=a[i];
	}
	return a;
}
public static void main(String[] args){
	int[] a={1,1,1,2,3,4};
	int[] result=remove(a);
	for(int i=0;i<result.length;i++)
System.out.println(result[i]);
	
}
}

- Anonymous February 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package EPIC;

import java.util.LinkedList;
import java.util.List;

public class ExtracUniqueElements {

	public static List<Integer> extractor(List<Integer> S) {
		List<Integer> R = new LinkedList<>();
		System.out.println(S.get(0));
		R.add(S.get(0));
		for (int i = 1; i <= S.size() - 1; i++) {

			if (S.get(i - 1) != S.get(i)) {
				System.out.println(S.get(i));
				R.add(S.get(i));
			}
		}
		return R;
	}

	public static void main(String[] args) {

		List<Integer> L = new LinkedList<>();

		L.add(1);
		L.add(1);
		L.add(3);
		L.add(3);
		L.add(3);
		L.add(5);
		L.add(5);
		L.add(5);
		L.add(5);
		L.add(9);

		System.out.println(L);

		List<Integer> r = extractor(L);
		System.out.println(r);

	}
}

- albertchenyu March 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ExtractUniqueWithoutUsingHashorSet {
public static void main(String[] args) {
int[] arr = {1,1,1,2,2,3,4,5,5,6,7,7,8,9,9};
for(int i = 0; i< arr.length-1 ; i++) {
if(arr[i] != arr[i+1]) {
System.out.println(arr[i]);
}
}
System.out.println(arr[arr.length-1]);
}
}

- Anonymous July 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

All O(n) solutions are incorrect as O(log n) solutions are possible. You do not need to check values between unique values as you know what they are. Hence, binary search that returns value when both start and end of split range are equal would give correct result much faster in cases where array is long and has only few unique values.

- Ithelog October 01, 2020 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More