Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

The question to be exact is find all pairs of strings in an array that when concatenated gives the same string. Condition is there are N elements(N is even) and there are definitely N/2 pairs. We need to find all the pairs and all pairs when concatenated with each other will give the same string.

For example : Array a = { "abc","efgh","abcde","fgh","abcd","defgh"};

Expected output : ("abc","defgh") , ("abcde","fgh") , ( "abcd","efgh")

- ur2cdanger September 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

First thing I feel might work... do two sorts... careful that second is stable:

1) Sort the array using string comparison
THEN
2) Stable sort the array using string length

Then pairs are:
first element with last
second element with second last
... etc...

- bigphatkdawg September 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Above works for your case (sort of increasing characters)

This would work better for general case:

Sort the array by size.
One of the first two elements is a prefix of one of the last two elements.

Let "s" be the one from the first two elements that is the prefix in above test.

Now walk down array checking pairs(a[0],a[1]) , (a[2],a[3]) ,...
One element of each pair has "s" as a prefix, just adjust each pair so the one with "s" as prefix comes in each pair.

Now you your final result is:
(first element of a, last element of a)
(second element of a, second last element of a)
... etc...

- bigphatkdawg September 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

* One element of each pair has "s" as a prefix, just adjust each pair so the one with "s" as prefix comes _first_ in each pair.

- bigphatkdawg September 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

To make it (more) failsafe.
Assume "t" was one of the first two elemetns that was a suffix of one of the last two.

When you rearrange each pair, make sure s is prefix of first one and t is suffix of last one.

And there might be more cases that squeek by, but you should be able to add it to the "rerrange pairs" linear scan.

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

I think we can youse graph algrithm approach to this problem. It's quite common in genome assbmling problem to build de Bruijn graph and find out Euler cycle on it. So let'sbuild graph, where strings are vertexes, and edge from u to v means that string corresponds to u contain suffix that also is preffix of string v. So the solution of this problem is to find Euler cycle on this graph.

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

One more idea to build suffix trie on this array. We have to concat all strings like s1$1s2$2..sn$n and insert specific delimeters, that will be unique in all string. Than we find lowest vertex, that contains under itself all strings. It's also popular problem how to find leats string, that contains another.

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

Longest common prefix problem. find the longest common prefix string and start find next half for the same.

- Gurukanth September 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

"common combinations" ? Ambiguous.
I think you just mean a string that contains all other strings as a permuted subsequence. Or put it another way, a string that is a superset of all characters appearing amoungst all the characters appearing in a.

Your examples are very unclear or basic. Example makes it seems like the strings come in pairs, and we have to add each pair (this would be easy also, a variant of below).

If you mean what I said at the very top, then simply count the occurences of letters as you check every string. So for example, if your encoding is ascii you can declare a 256 boolean/bit array, "seen", then:

For each string in a:
    For every valid letter c in the string:
          set seen[(int)c]=true;

For i=0 to 255:  
      if seen[i]==true: 
            print char(i); // or add char(i) to output result

- bigphatkdawg September 20, 2013 | 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