Epic Systems Interview Question for SDE1s


Country: United States
Interview Type: Phone Interview




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

void main()
{
        char a[] = "ACADBB123";
        char b[] = "DC1BA32BA";
        char temp;
        int n = sizeof(b);
        int j, i = 0;
        while(n > i) {
                        j = i;
                        printf("\n%s",a);
                        while(a[j] != b[i]) {
                                j++;
                        }
                        while(j > i) {
                                temp = a[j];
                                a[j] = a[j-1];
                                a[j-1] = temp;
                                printf("\n%s",a);
                                j--;
                        }
                        i++;
        }
}

- KRITK December 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice code..!!

- Anonymous February 15, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Nice code, but still has a small mistake. The "printf("\n%s",a);" (first print) should put before the "while(n>j)" (outer loop). Otherwise, it'll print some order twice.

- kira July 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

is there any effect on the output if we change the line while(a[j] != b[i]) to while(a[i] != b[j]) ??

- Anon October 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if you check the length of two input strings would be better. if the two length is not the same, then just stop.

- yuchaozh1991 October 15, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Similar idea. Shorter code. n is string length.

void adjSwap(char src[], char dst[], int n) {
	for (int cur = 0; cur < n; ++cur) {
		if (src[cur] == dst[cur]) continue;
		int ct = cur+1;
		for (; src[ct] != dst[cur] && ct < n; ++ct);
		for (; ct != cur; --ct) {
			swap(src[ct], src[ct - 1]);
			cout << src << endl;
		}
	}
}

The hard part is how to prove this is optimal. A similar problem is how to find how many swaps are necessary to convert one string to another using only adjacent swaps, which is also known as inversion count problem.

- XiaoPiGu December 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

(1)Do breath first seach, record each state's parent state during state extension.
(2)When reaching the target state, print the swaps using backtrace.
C++ code:

#include <vector>
#include <string>
#include <utility>
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;

void backtrace(vector<pair<string,int> >& queue, int parentIndex)
{
    if(parentIndex == -1) return;
    backtrace(queue, queue[parentIndex].second);
    cout << queue[parentIndex].first << "\n";
}
bool extend(vector<pair<string,int> >& queue, int& i, int levelCount, set<string>& record, const string& target)
{
    string s;
    for(; levelCount > 0; --levelCount, ++i){
        s = queue[i].first;
        for(string::size_type k = 1, len = s.size(); k < len; ++k){
            if(s[k] == s[k-1]) continue;

            swap(s[k], s[k-1]);
            if(s.compare(target) == 0){
                backtrace(queue, queue[i].second);
                cout << queue[i].first << "\n";
                cout << target << "\n";
                return false;
            }
            if(record.count(s) == 0){
                queue.push_back(make_pair(s, i));
                record.insert(s);
            }
            swap(s[k], s[k-1]);
        }
    }
    return true;
}
int transform(const string& start, const string& target)
{
    if(start.compare(target) == 0) return 0;

    set<string> record;
    vector<pair<string,int> > queue;
    queue.push_back(make_pair(start, -1));
    record.insert(start);

    int step = 0, i = 0, levelCount = 1;
    while(levelCount > 0){
        ++step;
        if(!extend(queue, i, levelCount, record, target)) break;
        else levelCount = queue.size() - i;
    }

    return step;
}

int main()
{
    string start = "GUM", target = "MUG";
    cout << "steps = " << transform(start, target);

    return 0;
}

- uuuouou December 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If there are no duplicate.
Associate int value characters, may be using hashtable
i.e. first word is GUM so
G = 0
U = 1
M = 2

Then do simple bubble sort on MUG with knowlege of associated int values.

- KnowledgeSeeker December 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>

using namespace std;

int main()
{
	int arr[10],ent,exp,temp=0;
	cin>>exp;
	for(int i=0;i<10;i++)
		arr[i]=0;
	
	temp=exp;
	while(temp)
	{
		arr[temp%10]++;
		temp=temp/10;
	}
	cin>>ent;
	temp=ent;
	while(temp)
	{
		arr[temp%10]--;
		temp=temp/10;
	}
	int count=0;
	for(int i=0;i<10;i++)
	{
		if(arr[i]!=0)
		count++;
	}
	if(count>1)
		cout<<"Invalid";
	else
		cout<<"Valid";
	
	
	return 0;
}

- crusader December 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

queue seems to me over kill here is how i would solve

static int Find(char ch, int startIndex, char[] str)
        {
            while (startIndex < str.GetLength(0) && str[startIndex] != ch)
            {
                startIndex++;
            }

            if (startIndex >= str.GetLength(0))
            {
                startIndex = -1;
            }

            return startIndex;
        }

        static void Swap(char[] str, int i, int j)
        {
            char temp = str[i];
            str[i] = str[j];
            str[j] = temp;
            Console.WriteLine("swap {0} {1} {2}", i, j, new string(str));
        }

        static void MoveCharToIndex(char[] str, char ch, int index)
        {
            int foundAt = Find(ch, index, str);

            while (foundAt > index)
            {
                Swap(str, foundAt, foundAt - 1);
                foundAt--;
            }

        }

        static void FindMoves(char[] target, char[] original)
        {
            if (target == null)
            {
                throw new ArgumentException("target");
            }
            if (original == null)
            {
                throw new ArgumentException("original");
            }

            //mush be same length

            if (target.GetLength(0) > 0)
            {

                int i = 0;

                while (i < target.GetLength(0))
                {
                    if (target[i] != original[i])
                    {
                        MoveCharToIndex(original, target[i], i);
                    }
                    i++;
                }

            }

        }

        static void FindMoves(string target, string original)
        {
            FindMoves(target.ToCharArray(), original.ToCharArray());
        }

        public static void Main()
        {
            FindMoves("GUM", "MUG");
            FindMoves("TEHUNOOL", "ONLEHTUO");

        }

- Anonymous December 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char[] s1 = str1.toCharArray();
		char[] s2 = str2.toCharArray();

		System.out.println( Arrays.toString(s1) );
		
		int n = s1.length;
		for( int i = 0;i < n; ++ i ){
			if( s1[i]!=s2[i] ){
				int j = i+1;
				for( ; j < n && s1[j] != s2[i]; ++ j );
				//now s1[j] == s2[i]
				for( ; j > i; -- j ){
					swap( s1, j, j-1 );
					System.out.println( Arrays.toString(s1) );
				}
			}
		}

- xiaoxipan December 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void revert(char arr[],int start,int end,int num){
		if(num>=arr.length/2) return;
		for (int i = end; i>=start; i--) {
			System.err.println(arr);
			char temp=arr[i];
			arr[i]=arr[i-1];
			arr[i-1]=temp;
			//System.err.println(arr);
		}
		for (int i = start; i <end; i++) {
			System.err.println(arr);
			char temp=arr[i];
			arr[i]=arr[i+1];
			arr[i+1]=temp;
		//	System.err.println(arr);
		}
		revert(arr, start+1, end-1,num+1);
	}

- TerrorGeek January 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;

public class Solution { 

	public static void main(String[] args) { 
		String s = "CGUM";
		String d = "MUCG";
		ArrayList<String> result = swap(s,d);
		for(int i=0;i<result.size();i++){
			System.out.println(result.get(i));
		}
	} 
	
	private static ArrayList<String> swap(String s,String d) { 
		ArrayList<String> result = new ArrayList<String>();
		result.add(s);
		char[] st = s.toCharArray();
		for(int i=0;i<s.length();i++){
			if(st[i]!=d.charAt(i)){
				for(int j=i+1;j<s.length();j++){
					if(st[j] == d.charAt(i)){
						for(int k = j;k>i;k--){
							char temp = st[k];
							st[k] = st[k-1];
							st[k-1] = temp;
							result.add(String.valueOf(st));
						}
					}
				}
			}
		}
		return result;
	} 

}

- Gary April 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I use recursive to solve this one base on permutation. Since we can swap 2 consecutive characters in the given String, the program will concatenate the first letter with the rest.
If the given String has length of 0 or 1, then return. Otherwise, make a recursive call to the substring s[1:] . Then add back first character to all possible position

# Using python
def permutation(lst):
    ''' take a list of element and return a list of its permutation'''
    if len(lst) == 0 or len(lst) == 1:
        return [lst]
    else:
        outlst = []
        for first in lst:
            rest = list(lst)
            rest.remove(first)
            for restlst in permutation(rest):
                outlst.append([first]+restlst)
        return outlst

def perString(s):
    lst = []
    retLst =[]
    for char in s:
        lst.append(char)
    ret = permutation(lst)
    for word in ret:
        show = ""
        for char in word:
            show += char
        retLst.append(show)
    return retLst

Result:

>>> perString('GUM')
['GUM', 'GMU', 'UGM', 'UMG', 'MGU', 'MUG']

- phuocidi July 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args){
char[] s = "abcdefg".toCharArray();
int n = s.length;
swapchar(s,n);
}
public static void swapchar(char[] s,int n){
int length = s.length;
if(n==0)
{
for(int i =0;i<length;i++)
System.out.print(s[i]);
System.out.println();
}else{
char c;
for(int i =0;i<n-1;i++){
c = s[i];
s[i] = s[i+1];
s[i+1] = c;
for(int j =0;j<length;j++)
System.out.print(s[j]);
System.out.println();
}
n = n-1;
swapchar(s,n);
}
}

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

public class SwapLetters {
	
	public static void main(String[] args) {
		String s = "ABCD";
		char[] inArray = s.toCharArray();
		for (int i = 0; i < inArray.length; i++) {
			for (int j = 0; j < inArray.length-1-i; j++) {
				char temp = inArray[j];
				inArray[j] = inArray[j+1];
				inArray[j+1] = temp;
				System.out.println(new String(inArray));
			}
		}
	}

}

- tssr September 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package practice;

import java.sql.Array;

public class String_swap {
public static void main(String Args[])
{
String source="mug";
String dis=new String();
String target="gum";
String source1=new String();
char[] ar=source.toCharArray();
char[] br=target.toCharArray();
int pos=source.indexOf(target.charAt(0));
char c=target.charAt(0);
System.out.println(ar);
int p=0;
for(int i=0;i<source.length()-1 ;i++)
{
	swap(ar,br,pos,p,c);
	c=target.charAt(i+1);
	pos=source.indexOf(target.charAt(i));
	p=i+1;
	
}
//System.out.println(dis);
}
static char[] swap(char[] ar,char[] br, int pos,int p,char c)
{   char temp1,temp2;
	
	while(ar[p]!=c)
	{
			temp1=ar[pos];
			temp2=ar[pos-1];
			ar[pos-1]=temp1;
			ar[pos]=temp2;
			System.out.println(ar);
			pos--;
	}
	return ar;
	
}
}

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

void permutate(char str[])
{
     int l = strlen(str);
     if (l ==0 || 1 == l)
        return;
     int i, j;
     for (i = 0; i< l-1;i++)
     {
         for (j = 0; j<l-1; j++)
             if (str[j] !=str[j+1])
             {
                        char c = str[j];
                        str[j] = str[j+1];
                        str[j+1] = c;
                        printf("\n %s", str);
             }
     }
     
}

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

Written in python 3

def scramble(old, new):
    old = list(old); new = list(new)
    print(''.join(new))
    for j in range(len(old)):
        letter = old[j]
        split1 = new[:j]; split2 = new[j:] 
        location = split2.index(letter) #finds location of ith letter in new word
        for i in reversed(range(1, location + 1)):
            temp = split2[i]
            split2[i] = split2[i-1]
            split2[i-1] = temp
            print(''.join(split1 + split2))
        new = split1 + split2

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

recursive solution

public static void doAllSteps(char [] chs,int lastPre,int last,String target){
		if (lastPre<0){
			lastPre =chs.length-2;
			last= lastPre +1 ;
		}
		
		char tmp = chs[last];
		chs [last] = chs[lastPre];
		chs[lastPre] = tmp;
		if(target.equals(new String(chs))){
			System.out.println(new String(chs));
			return ;
		}else{
			System.out.println(new String(chs));
		}
		lastPre--;
		last--;
		
		doAllSteps(chs,lastPre,last,target);
		
	}
	
	public static void AllSteps(String src, String target){
		System.out.println(src);
		doAllSteps(src.toCharArray(),target.length()-2,target.length()-1,target);
	}

- Anonymous December 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 6 vote

char[] s1 = "TEHUNOOL".toCharArray();
	char[] s2 = "ONLEHTUO".toCharArray();

	transpose(s1, s2);

     public static void transpose(char[] s1, char[] s2) {

	int i = 0;
	System.out.println(Arrays.toString(s1));
	System.out.println(Arrays.toString(s2) + "\n");
	while (i < s2.length) {
	    if (s2[i] == s1[i]) {
		i++;
		System.out.println(Arrays.toString(s1));
	    } else {
		int j = i;
		while (j < s1.length - 1 && s2[i] != s1[i]) {
		    char temp = s1[j];
		    s1[j] = s1[j + 1];
		    s1[j + 1] = temp;
		    j++;
		}
	    }
	}
	System.out.println("\n" + Arrays.toString(s1));
	System.out.println(Arrays.toString(s2));
    }

- thelineofcode December 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

package com.test;

import java.util.Arrays;

public class ReverserStr {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		char[] s1 = "TEHUNOOL".toCharArray();
		char[] s2 = "ONLEHTUO".toCharArray();

		int i = 0,counter=0;
		System.out.println(Arrays.toString(s1));
		System.out.println(Arrays.toString(s2) + "\n");
		while (i < s2.length) {
			if (s2[i] == s1[i]) {
				i++;
				System.out.println(Arrays.toString(s1));
				counter++;
			} else {
				for (int j = i; j < s1.length - 1; j++) {
					counter++;
					char temp = s1[j];
					s1[j] = s1[j + 1];
					s1[j + 1] = temp;
				}
			}
		}
		System.out.println("\n" + Arrays.toString(s1));
		System.out.println(Arrays.toString(s2));
		System.out.println(counter);
	}
	
/*	public static void main(String[] args) {
		char[] s1 = "TEHUNOOL".toCharArray();
		char[] s2 = "ONLEHTUO".toCharArray();

		int i = 0,counter=0;
		System.out.println(Arrays.toString(s1));
		System.out.println(Arrays.toString(s2) + "\n");
		while (i < s2.length) {
			if (s2[i] == s1[i]) {
				counter++;
				i++;
				System.out.println(Arrays.toString(s1));
			} else {
				for (int j = i; j < s1.length; j++) {
					counter++;
					if(s2[i] == s1[j]) {
						char temp = s1[j];
						s1[j] = s1[i];
						s1[i] = temp;
						System.out.println(Arrays.toString(s1));
						i++;
						break;
					}
				}
			}
		}
		System.out.println("\n" + Arrays.toString(s1));
		System.out.println(Arrays.toString(s2));
		System.out.println(counter);
	}*/

}

- optimzed way to do same task December 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

None of these work.

- CodeNinja March 07, 2014 | Flag


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