Strings : String Representation Of The Integer


String Representation of the Integer HiringLibrary-Com

Write a function that takes an int and returns a chararray containing the string representation of the integer.

Rationale: Assume that the chararray has enough space to contain the int. Use the following function header:

void convert(int x, char * buf) {};

Perhaps the trickiest part of this problem is to handle negative numbers. All we need to do in this case, however, is to multiply the number by -1 and remember that its initial value was negative. The algorithm we will be using performs the following steps:

  • Start with an empty string;
  • Take every digit of the integer one by one, starting with the least significant one, and append it to the string;
  • If the number was initially negative append a ‘-‘ character to the string
  • Reverse the string.

For example, given the input -318513513, we would add 3,1,5,3,1,5,8,1,3 and – in the string and then reverse it, producing the initial number.

Before solving this problem do the following (not limited to):

  • Clarify doubts
  • Write/loudly speak/analyze single or multiple pseudocode(e.g. trivial algorithm to advanced algorithm/Data Structures) which can lead to solution
  • Explain any assumptions or limitations on the written pseudocode
  • Mention major data structures to be used
  • Also how the positive/negative functional tests prove that the resultant solution is correct. 

Professional Teacher at

Clarification Points 

Clarification Point-1:

What is the maximum size of the number? If it’s more than 10, we may need to use the type long instead of integers.

Clarification Point-2:

Can the number be negative? Does the sign need to be stored in the string?


Diversity in WorkPlace - Japanese,Korea,Chinese

Pseudo Code

Pseudo code:

Convert(x, buf)
  If x is negative
    x = x * -1
    remember x was negative
  while there are digits in x
    digit = last digit of x
    buf = buf + digit;
    x = x / 10
  if x was initially negative
    buf = buf + ‘-‘
  reverse buf


How did you find your first job?



  • We will assume the given number is an integer.
  • Our program will also handle negative integers.


Career Summary from a Professional for HiringLibrar

Major Data Structures 

Major data structures:

Array of characters (string).


Example at Hiring Library

Unit tests:  
Postive Functional Test cases  
Input Expected Output
X = 1 “1”
X = 0

X = 1234

X = -12345





Negative Functional Test Cases
313158205942551 Overflow

Answer of the coding Problem

Here is the sample answer that implements the solution described above

#include <cstdio>
#include <string.h>

using namespace std;

char array[100];

void convert(int x, char *buf)
    // remember if the number is negative, so we can add '-' at the end
  bool negative = false;
  if (x < 0)
      negative = true;
        x *= -1;

    // append digits to array of chars one by one, in reverse order
  int digit;
    digit = x % 10;
    buf[strlen(buf)] = digit + '0';
    x = x / 10;

    // add the '-' if necessary
  if (negative)
    buf[strlen(buf)] = '-';

    // reverse the string

int main()
  int number = -318513513;

  convert(number, array);
  printf("Here is the converted number: %s\n", array);
  return 0;

Output at HiringLibrary-Com

Output and Further Excercise

Further Exercise at HiringLibrary

Output: Further exercise:

Here is the converted number:        -318513513


Q: How to we convert the number back to its integer representation?

A: We iterate through the string, and add them to an integer, initially set to 0. For example, given “1443” we would do the following steps:

Assign 1 to the integer, multiply it by 10, add 4, multiply by 10, add 4, and so on. We also need to keep track of the sign and multiply the integer in the end.