Implement Printf
Updated
Problem statement
Write a small text-formatting function, like a simple printf. You get a string pattern and a list of strings values. In this exercise, the pattern can use only three codes: %s, %d, and %%.
- Every other character in
patternis copied to the output as it is. %stakes the next string fromvaluesand puts it in exactly as written.%dtakes the next string, which holds a signed 32-bit decimal integer. Write that number in its standard decimal form: drop a leading plus sign and any extra leading zeros, and write zero as0.%%writes one percent sign and takes no value.- Only the codes in the original pattern are read. A percent sign inside an inserted value is plain output text and is never read as a code.
Return the resulting string. Every pattern is valid: each percent sign in it is part of one of the three two-character codes above. The number of %s and %d codes together equals values.length. Width, precision, and other codes are not part of this exercise.
Function
formatPrintf(pattern: String, values: String[]) → StringExamples
Example 1
pattern = "%s scored %d%%"values = ["Ada","+007"]return = "Ada scored 7%"%s puts in Ada. %d reads the next value, drops its plus sign and leading zeros, and writes 7. The final %% adds one percent sign at the end.
Example 2
pattern = "[%s] %d"values = ["%d%%","-0000"]return = "[%d%%] 0"%s puts in the value %d%% exactly as written. Those percent signs are now plain text, so they are not read again. Then %d reads negative zero with extra zeros and writes it as 0.
Example 3
pattern = "%%%%"values = []return = "%%"The pattern is two %% codes in a row. Each one writes a single percent sign and takes no value, so the result is two percent signs.
Constraints
0 <= pattern.length <= 10^5.0 <= values.length <= 10^5.- All strings contain only printable ASCII characters; an argument used by
%smay be empty. - An argument used by
%dcontains an optional+or-followed by between1and10decimal digits, and its value lies from-2147483648through2147483647. - The total number of characters across all values is at most
10^5, and the returned string has at most2 * 10^5characters.
More NVIDIA Onsite Interview questions
- Check Endianness Of A Byte ArrayONSITE INTERVIEW · Seen Sep 2026
- Implement MallocONSITE INTERVIEW · Seen Sep 2026
- Merge Two Arrays Into Sorted OrderONSITE INTERVIEW · Seen Sep 2026
- Middle Node Of A Linked ListONSITE INTERVIEW · Seen Sep 2026
- Ping Reply TrackingONSITE INTERVIEW · Seen Sep 2026
- Reverse A StringONSITE INTERVIEW · Seen Sep 2026
- Maximum Fish in a GridONSITE INTERVIEW · Seen Sep 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- First Register Overflow TickONSITE INTERVIEW · Seen May 2024
- Two-Egg DroppingONSITE INTERVIEW · Seen May 2024
Similar questions at other companies
- Shortest Uncommon SubstringAirbnb · ONSITE INTERVIEWAirbnb interview questions
- Feature Flags with Rules, Dependencies and ExposurePerplexity AI · ONSITE INTERVIEWPerplexity AI interview questions
- Longest Substring Without Repeating CharactersOracle · ONSITE INTERVIEWOracle interview questions
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Longest Substring Without Repeating CharactersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Classify Password StrengthHackerRank · PHONE SCREEN · ONSITE INTERVIEWHackerRank interview questions
- Sliding Items on a Circular RingIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Longest Substring Without Repeating CharactersMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Stateful Log Transform and SearchRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions