16 Replies - 3760 Views - Last Post: 10 March 2011 - 02:11 PM
#1
Number 1 Interview Question
Posted 08 March 2011 - 10:33 PM
Show me how you would reverse a string.
With that said, thoughts on this question and how would YOU reverse a string in your prefered language?
Replies To: Number 1 Interview Question
#2
Re: Number 1 Interview Question
Posted 08 March 2011 - 10:50 PM
nimajneb, on 09 March 2011 - 12:33 AM, said:
Determine the length of the string
Dump the strings value into an array
Set an integer variable to the length
Run through the values comparing the current value of the variable
Subtract for each instance of the loop
Repeat until the integer variable equals zero, outputing each element of the array to a 2nd array, or to standard output, depending on the request.
I have to admit, I've never been asked this during an interview. The question I always got asked most often is "Where do you see yourself within the company in five years?".
#3
Re: Number 1 Interview Question
Posted 08 March 2011 - 11:00 PM
#4
Re: Number 1 Interview Question
Posted 08 March 2011 - 11:37 PM
P.S. By the way, this reminded me of one of the Google interviews. I had to answer about 4 algorithmic questions in 10 minutes. And one of the questions was something like this:
Say you have a singly linked list of N elements. You need to *append* to it the *reverse* of its first (N-1) elements.
So if you have A->B->C->D
Turn it into A->B->C->D->C->B->A
This post has been edited by Nikitin: 08 March 2011 - 11:51 PM
#5
Re: Number 1 Interview Question
Posted 09 March 2011 - 06:14 AM
#6
Re: Number 1 Interview Question
Posted 09 March 2011 - 06:23 AM
This post has been edited by codeprada: 09 March 2011 - 06:23 AM
#7
Re: Number 1 Interview Question
Posted 09 March 2011 - 06:44 AM
tlhIn`toq, on 09 March 2011 - 03:14 PM, said:
http://stackoverflow...string-in-c-2-0
I personally would go with this:
public string Reverse(string text)
{
if (text == null) return null;
// this was posted by petebob as well
char[] array = text.ToCharArray();
Array.Reverse(array);
return array;
}
#8
Re: Number 1 Interview Question
Posted 10 March 2011 - 12:59 AM
something like:
void reverse(char *str, int len) {
char tmp;
int swap_index;
for (int i = 0;i < (len /2);i++) {
swap_index = len - (i + 1);
tmp = str[swap_index];
str[swap_index] = str[i];
str[i] = tmp;
}
}
This post has been edited by Dark_Nexus: 10 March 2011 - 01:18 AM
#9
Re: Number 1 Interview Question
Posted 10 March 2011 - 04:51 AM
void reverse(char *s, int len) {
char *head = s, *tail = (s+len-1);
while(head<tail) {
char temp = *head;
*head++ = *tail;
*tail-- = temp;
}
}
Or, for the non pointer crowd:
void reverse(char s[], int len) {
for(int head = 0, tail=len-1; head<tail; head++, tail--) {
char temp = s[head];
s[head] = s[tail];
s[tail] = temp;
}
}
You should be able to write this in assembly with little trouble.
The goal is as few operations as possible; overall. Calling builtins can be very expensive, depending on what you're doing. Incrementing and decrementing are basic, so is compare. The more functions you pull in, the more overhead. Of course, the design philosophy of the language may be that I shouldn't have to worry about such things.
If you don't care about memory, then loading into another array would be acceptable. Indeed, it could be preferable.
This is more a question of language than approach. The above is valid for your run of the mill procedural language. Others might have more clever ways. A good functional language would use tail end recursion.
#10
Re: Number 1 Interview Question
Posted 10 March 2011 - 05:18 AM
import java.util.Scanner;
public class Reverse {
public static void main(String[] args){
Scanner read = new Scanner(System.in);
String word;
System.out.println("Enter the word you would like to reverse:");
word = read.next();
char wrd[] = word.toCharArray();
int len = wrd.length;
for(int i = len - 1; i >= 0; i--){
System.out.print(wrd[i]);
}
System.out.println();
}
}
This post has been edited by macosxnerd101: 10 March 2011 - 05:54 AM
Reason for edit:: Please use code tags
#11
Re: Number 1 Interview Question
Posted 10 March 2011 - 05:30 AM
#12
Re: Number 1 Interview Question
Posted 10 March 2011 - 05:41 AM
Nov1ce, on 10 March 2011 - 05:18 AM, said:
import java.util.Scanner;
public class Reverse {
public static void main(String[] args){
Scanner read = new Scanner(System.in);
String word;
System.out.println("Enter the word you would like to reverse:");
word = read.next();
char wrd[] = word.toCharArray();
int len = wrd.length;
for(int i = len - 1; i >= 0; i--){
System.out.print(wrd[i]);
}
System.out.println();
}
}
Hey you didn't have to vote me down I didn't know. Am kinda new!!!
#13
Re: Number 1 Interview Question
Posted 10 March 2011 - 06:08 AM
#14
Re: Number 1 Interview Question
Posted 10 March 2011 - 11:03 AM
#15
Re: Number 1 Interview Question
Posted 10 March 2011 - 11:23 AM
|
|

New Topic/Question
Reply



MultiQuote








|