Number 1 Interview Question

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 6528 Views - Last Post: 10 March 2011 - 02:11 PM

#1 nimajneb  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 116
  • Joined: 04-August 10

Number 1 Interview Question

Posted 08 March 2011 - 10:33 PM

So I've been doing software dev recruiting for a few months now and wanted to share something... after holding countless interviews and sitting in on MANY company/candidate interviews, one question seems to always come up in whiteboard sessions:

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?
Is This A Good Question/Topic? 0
  • +

Replies To: Number 1 Interview Question

#2 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5247
  • View blog
  • Posts: 27,069
  • Joined: 10-May 07

Re: Number 1 Interview Question

Posted 08 March 2011 - 10:50 PM

View Postnimajneb, on 09 March 2011 - 12:33 AM, said:

With that said, thoughts on this question and how would YOU reverse a string in your prefered language?

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?".
Was This Post Helpful? 0
  • +
  • -

#3 nimajneb  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 116
  • Joined: 04-August 10

Re: Number 1 Interview Question

Posted 08 March 2011 - 11:00 PM

I think the point of the question is creativity and memory handling. I've seen some people completely blow it.
Was This Post Helpful? 0
  • +
  • -

#4 Nikitin  Icon User is offline

  • D.I.C Regular

Reputation: 56
  • View blog
  • Posts: 264
  • Joined: 02-August 10

Re: Number 1 Interview Question

Posted 08 March 2011 - 11:37 PM

Is this like a first-round weed out question? What are some of the harder ones you ask?

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

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Number 1 Interview Question

Posted 09 March 2011 - 06:14 AM

These guys covered it pretty much to death.
http://stackoverflow...string-in-c-2-0
Was This Post Helpful? 0
  • +
  • -

#6 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Number 1 Interview Question

Posted 09 March 2011 - 06:23 AM

I always get asked...Describe yourself for me. I had one programming job and i had to make a form and mail it in php.

This post has been edited by codeprada: 09 March 2011 - 06:23 AM

Was This Post Helpful? 0
  • +
  • -

#7 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Number 1 Interview Question

Posted 09 March 2011 - 06:44 AM

View PosttlhIn`toq, on 09 March 2011 - 03:14 PM, said:

These guys covered it pretty much to death.
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;
}

Was This Post Helpful? 0
  • +
  • -

#8 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: Number 1 Interview Question

Posted 10 March 2011 - 12:59 AM

I would just walk through the string swapping characters at 0 and len -1, then 1 and len -2, and so on until the string was reversed. This allows you to reverse the string in place and not consume any additional memory besides a character for swaping.

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

Was This Post Helpful? 1
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,637
  • Joined: 16-October 07

Re: Number 1 Interview Question

Posted 10 March 2011 - 04:51 AM

Probably the most efficient, for the operations and storage, is to start at both ends:
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.
Was This Post Helpful? 1
  • +
  • -

#10 Nov1ce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-March 11

Re: Number 1 Interview Question

Posted 10 March 2011 - 05:18 AM

Waz able to crack it using Java. I wanted to use C++ but I was having problems declaring strings and I did not want to use 'Char'. So if you got an idea on how to declare a string right-away I would appreciate (I use dev compiler). Anyways, this is the code::

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();
    }
}


:bananaman: Don't forget to click the green button!!!

This post has been edited by macosxnerd101: 10 March 2011 - 05:54 AM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5480
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: Number 1 Interview Question

Posted 10 March 2011 - 05:30 AM

Even here you should use code tags
:code:
Was This Post Helpful? 0
  • +
  • -

#12 Nov1ce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-March 11

Re: Number 1 Interview Question

Posted 10 March 2011 - 05:41 AM

View PostNov1ce, on 10 March 2011 - 05:18 AM, said:

Waz able to crack it using Java. I wanted to use C++ but I was having problems declaring strings and I did not want to use 'Char'. So if you got an idea on how to declare a string right-away I would appreciate (I use dev compiler). Anyways, this is the code::

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();
    }
}


:bananaman: Don't forget to click the green button!!!


Hey you didn't have to vote me down I didn't know. Am kinda new!!!
Was This Post Helpful? 0
  • +
  • -

#13 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: Number 1 Interview Question

Posted 10 March 2011 - 06:08 AM

I think the point is to reverse a string the most efficient way. Not to just display it backward in console but nice try ^^
Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: Number 1 Interview Question

Posted 10 March 2011 - 11:03 AM

1. Why do want to work us?
Was This Post Helpful? 0
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: Number 1 Interview Question

Posted 10 March 2011 - 11:23 AM

String Reversal in .net
Spoiler

Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2