Interview Question


Country: United States




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

In this solution I expect that duplicates could be anywhere. Linear complexity:

public static String findDuplicates(String duplicates){
        final Set<Character> chars = new HashSet<>();
        final StringBuilder result = new StringBuilder();

        for(Character c: duplicates.toCharArray()){
            if(!chars.add(c)) {
                result.append(c);
            }
        }

        return result.toString();
    }

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

If chars repeat more than twice, the output String will have the character more than once.
input : AAAa
output : AA
Since the chars.add of 'A' will be false twice in the above case, StringBuilder append will be run twice for the same 'A' character.
We should use Set to store the result String.

- Arun September 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If a character repeats more than twice, it will be added more than once in the output String.
eg input : AAAaBBB
output : AABB
We should use Set to store the output.

- arun.theprofessional September 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I hope this will work, Written in C#, I do not know anything about time& space complexity.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AlgorithmPractice
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "AABBCcDdww";
            char[] data = input.ToCharArray();
            char[] charHolder = new char[52];
            int i = 0;
            foreach (char item in data)
            {
                if (charHolder.Contains(item))
                  Console.Write(item);
                else
                    charHolder[i++] = item;
                
            }
            Console.ReadLine();
        }
    }
}

- Ramakrishnan September 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can have two pointers here _lastchar and _newChar

char[] charArray = input.ToCharArray();
                List<char> list = new List<char>();


                char _lastChar = charArray[0];
                for (int i = 1; i < charArray.Length; i++)
                {
                    char _newchar = charArray[i];

                    if (_newchar != _lastChar)
                    {
                        if (((int)_newchar - (int)_lastChar) == 32 || ((int)_newchar - (int)_lastChar) == -32)
                        {
                            _lastChar = _newchar;

                            for (int j = i; j < charArray.Length; j++)
                            {
                              if(charArray[j]!=_lastChar && !list.Contains(_lastChar))
                              {
                                  i = j;
                                  _lastChar = charArray[j];
                                  break;

                              }
                            }
                        }
                        
                        else
                        {
                            list.Add(_lastChar);
                            _lastChar = _newchar;
                        }

                        
                    }
                    else if (i == charArray.Length-1 && _newchar == _lastChar)
                    {
                        list.Add(_lastChar);
                    }
                }

- Kyle September 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char[] charArray = input.ToCharArray();
                List<char> list = new List<char>();


                char _lastChar = charArray[0];
                for (int i = 1; i < charArray.Length; i++)
                {
                    char _newchar = charArray[i];

                    if (_newchar != _lastChar)
                    {
                        if (((int)_newchar - (int)_lastChar) == 32 || ((int)_newchar - (int)_lastChar) == -32)
                        {
                            _lastChar = _newchar;

                            for (int j = i; j < charArray.Length; j++)
                            {
                              if(charArray[j]!=_lastChar && !list.Contains(_lastChar))
                              {
                                  i = j;
                                  _lastChar = charArray[j];
                                  break;

                              }
                            }
                        }
                        
                        else
                        {
                            list.Add(_lastChar);
                            _lastChar = _newchar;
                        }

                        
                    }
                    else if (i == charArray.Length-1 && _newchar == _lastChar)
                    {
                        list.Add(_lastChar);
                    }
                }

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

char[] charArray = input.ToCharArray();
                List<char> list = new List<char>();


                char _lastChar = charArray[0];
                for (int i = 1; i < charArray.Length; i++)
                {
                    char _newchar = charArray[i];

                    if (_newchar != _lastChar)
                    {
                        if (((int)_newchar - (int)_lastChar) == 32 || ((int)_newchar - (int)_lastChar) == -32)
                        {
                            _lastChar = _newchar;

                            for (int j = i; j < charArray.Length; j++)
                            {
                              if(charArray[j]!=_lastChar && !list.Contains(_lastChar))
                              {
                                  i = j;
                                  _lastChar = charArray[j];
                                  break;

                              }
                            }
                        }
                        
                        else
                        {
                            list.Add(_lastChar);
                            _lastChar = _newchar;
                        }

                        
                    }
                    else if (i == charArray.Length-1 && _newchar == _lastChar)
                    {
                        list.Add(_lastChar);
                    }

}

- kyle September 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.HashMap;


public class e3 {

public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer();
String s = "AABBCcDdww";
char[] c = s.toCharArray();
ArrayList<Character> a = new ArrayList<>();
for(char x : c){
if(a.contains(x)){
sb.append(x);

}
else{
a.add(x);
}
}
System.out.println(sb.toString());


}

}

- priya September 27, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this one works in c++
don't know the speed but its using the maps indexing instead of additional loops so I imagine it's fairly quick.

string FindDuplicateChars(string str)
{
	hash_map<char, int> dupMap;
	string dups;

	// loop through the string
	for(auto const& curChar : str)
	{
		// if emplace fails for first time (== 0)
		if(!dupMap.emplace(curChar, 0).second && dupMap[curChar] == 0)
		{
			// add curChar to 'unique' string and change dupMap value
			dups += string(&curChar, sizeof(char));
			dupMap[curChar] = 1;
		}
	}
	return dups;
}

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

this one works in c++
don't know the speed but its using the maps indexing instead of additional loops so I imagine it's fairly quick.

string FindDuplicateChars(string str)
{
	hash_map<char, int> dupMap;
	string dups;

	// loop through the string
	for(auto const& curChar : str)
	{
		// if emplace fails for first time (== 0)
		if(!dupMap.emplace(curChar, 0).second && dupMap[curChar] == 0)
		{
			// add curChar to 'unique' string and change dupMap value
			dups += string(&curChar, sizeof(char));
			dupMap[curChar] = 1;
		}
	}
	return dups;
}

- gscottiv September 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def duplicates(s):
    count = defaultdict(int)
    for c in s:
        count[c] += 1
    return "".join([c for c in count if count[c] > 1])

- yehadut September 29, 2015 | 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