Interview Question






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

The best way to tackle these customized sorts is to provide a Comparator function (solution below is in Java. I guess in C++ also you can do this)

So Comparator would be :

Comparator<String> reverseComparator = new Comparator<String>() {
public int compare(String s1, String s2) {
String rev1 = //reverse the first String s1 here;
String rev2 = //reverse the first String s2 here;

return rev1.compareTo(rev2);

}

};

List<String> originalStringList = Arrays.asList("xxxB", "yyyC", "zzzA");

Collections.sort(originalStringList, reverseComparator);

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

Use qsort and define the compare function such that it compares the strings in reverse order starting with the last character.

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

#include<iostream>
#include<string>
using namespace std;
char *arr[] = {"xxxB","yyyC","zzzA"};
void sort()
{
for(int i = 0; i < 3 ; i++)
{
for(int j = i+1; j < 3 ; j++)
{
if(strcmp(arr[i],arr[j])> 0)
{
char *temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
void reverse(char *p,int len)
{
char arrev[strlen(p)];
int i = strlen(p)- 1;
int y = 0;
int j = len;
int count = strlen(p);
p = p + strlen(p)- 1;

while(i >=0)
{
arrev[y] = *p;
*p--;
i-- ;
y++;
}
arrev[count]= '\0';
strcpy(arr[j],arrev);
}

int main()
{

for (int i = 0; i < 3 ; i++)
{
reverse(arr[i],i);
}
sort();
for (int i = 0; i < 3 ; i++)
{
reverse(arr[i],i);
}
for (int i = 0; i < 3 ; i++)
{
cout<<"arr=="<<arr[i]<<endl;
}
return 0;

}

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

I cant find any solution better than reversing, sorting and reversing again :(

- BadCoder January 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can we use java ?

- Ryan January 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about:
1) Create an array of reversed strings with pointers to original strings.
2) Sort the newly created array.
3) Use the pointers to iterate through the original array.
4) This will save the cost of reversing the strings every time we do comparisons.

- Aditya January 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String reverse(String str){
StringBuilder sb = new StringBuilder();
for(int i=str.length()-1; i>=0; i--)
sb.append(str.charAt(i));
return sb.toString();
}

public static void main(String[] args) {
String[] arr = {"zzzC", "yyyB", "xxxA"};
HashMap<String, String> hm = new HashMap<String, String>();
for(int i=0; i< arr.length; i++){
hm.put(reverse(arr[i]),arr[i]);
}
ArrayList<String> tmp = new ArrayList<String>(hm.keySet());
Collections.sort(tmp);

ArrayList<String> result = new ArrayList<String>();
for(String str: tmp){
result.add(hm.get(str));
}

Object[] resultArr = result.toArray();
for (int i = 0; i < resultArr.length; i++)
System.out.println((String) resultArr[i]);

}

- jimmywang052114 January 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String reverse(String str){
StringBuilder sb = new StringBuilder();
for(int i=str.length()-1; i>=0; i--)
sb.append(str.charAt(i));
return sb.toString();
}

public static void main(String[] args) {
String[] arr = {"zzzC", "yyyB", "xxxA"};
HashMap<String, String> hm = new HashMap<String, String>();
for(int i=0; i< arr.length; i++){
hm.put(reverse(arr[i]),arr[i]);
}
ArrayList<String> tmp = new ArrayList<String>(hm.keySet());
Collections.sort(tmp);

ArrayList<String> result = new ArrayList<String>();
for(String str: tmp){
result.add(hm.get(str));
}

Object[] resultArr = result.toArray();
for (int i = 0; i < resultArr.length; i++)
System.out.println((String) resultArr[i]);

}

- jimmywang052114 January 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about Radix Sort ? sort it with starting from the left most character . . . .

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

Why to reverse each string?
We can compare 2 strings character by character from end to start.

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

This can be done using trie data structure.Insert the reverse of words in trie and printing the word in reverse order.

- kewl September 10, 2011 | 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