PPractick

DSA interview question

Reading Input and Printing Output (Input / Output Basics)

Easy · Algorithms · Topics: language basics

The problem

You're writing the first piece of a check-in tool for a college fest. At the registration desk each attendee types three things: their age, their full name (which may contain spaces), and their height in metres. Your program must read these three values and print one formatted badge line. It sounds trivial, but almost every beginner's first bug lives here: the name comes out empty, or only the first word is read, or the height prints with ten decimal places. Getting input and output exactly right is also what online assessments silently test on every single problem, since your code has to parse the input format itself.

Example: input 21, Riya Sharma, 1.6 (one value per line) should print Name: Riya Sharma, Age: 21, Height: 1.60 m.

Function signature

void readAndPrintBadge();   // reads age, full name, height from stdin; prints one line to stdout

Test cases

Input (one value per line)OutputExplanation
21 / Riya Sharma / 1.6Name: Riya Sharma, Age: 21, Height: 1.60 mName has a space, so it must be read as a whole line
18 / Arjun / 1.8Name: Arjun, Age: 18, Height: 1.80 mHeight padded to exactly 2 decimals
45 / Dr. Meera Iyer / 1.627Name: Dr. Meera Iyer, Age: 45, Height: 1.63 mThree-word name; height rounded to 2 decimals
0 / A / 0.5Name: A, Age: 0, Height: 0.50 mSmallest valid values

(Confirm: the name is on its own line and may contain spaces; height should always print with exactly 2 decimal places.)

How interviewers phrase it

Clarifying questions to ask

  1. What exactly is the input format: one value per line, or space-separated on a single line?
  2. Can the name contain spaces? (This decides between cin >> and getline.)
  3. How should decimals be printed: fixed number of places, and rounded or truncated?
  4. How large can the input get? (Decides whether fast I/O matters.)
  5. Is the number of inputs given up front, or do I read until end of file?

Follow-ups to expect

  1. "Why does getline return an empty string right after cin >> age?"
  2. "Your program is slow on a million numbers. Why?"
  3. "What's the difference between endl and '\n'?"
  4. "How do you read numbers until the input ends?"
  5. "How do you print a double with exactly two decimals?"
  6. "How do you read a line of numbers when you don't know how many there are?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in