In the TCS NQT (Ninja and Digital roles), the coding section has specific constraints you must know before writing a single line of code.
stdin (Standard Input) and print output to stdout (Standard Output). Do not write input prompts (e.g., don't print "Enter a number:").Input: 9875 → 9+8+7+5=29 → 2+9=11 → 1+1=2.
Concept: Recursion or formula 1 + (num-1)%9.
Asked in: TCS NQT Foundation (Slot 1, 2, 3 – March 2021) Tcs Coding Questions 2021
Problem Statement:
A scientist has discovered a new number system where the base is 2. Write a program to convert a given decimal number (N) into its binary equivalent. However, you cannot use the bin() function or inbuilt conversion libraries. Additionally, find the number of '1's in the binary representation (Hamming Weight).
Example:
Approach (Python):
def decimal_to_binary(n): binary = "" if n == 0: binary = "0" while n > 0: binary = str(n % 2) + binary n //= 2 # Count ones count_one = binary.count('1') return binary, count_one
num = int(input()) b, c = decimal_to_binary(num) print(b) print(c)In the TCS NQT (Ninja and Digital roles),
Before diving into specific questions, you must understand the battlefield. Input/Output: You must read input from stdin (Standard
Key Insight from 2021: TCS shifted focus from complex data structures (like graphs) to optimization problems involving prime numbers, base conversions, and string manipulations.