ProgrammingHardProduct company OA
What is printed?
What is printed?
int ok(const char *s, int d) {
if (d < 0) return 0;
if (!*s) return d == 0;
return ok(s + 1, d + (*s == '(') - (*s == ')'));
}
int main(void) {
printf("%d%d%d%d", ok("(()())", 0), ok("())(", 0), ok("((", 0), ok("", 0));
}- A. 1000
- B. 0001
- C. 1101
- D. 1001
Correct answer: D. 1001
d tracks open brackets. "(()())" never goes negative and ends at 0 → 1. "())(" drops to −1 at the third character and fails even though its counts balance → 0. "((" ends at 2 → 0. The empty string ends at 0 → 1. '1101' checks only the totals.
More C Programming questions
- What is printed? c int n = 1200, r = 0; while (n) { r = r 10 + n % 10; n /= 10; } printf("%d", r);
- What is printed? c enum { R = 4, W = 2, X = 1 }; int perm = R | X; perm |= W; perm &= ~R; perm ^= X; printf("%d %d", perm, (perm & W) != 0);
- What is printed? c printf("%d", 5["abcdef"] - 'a');
- What is printed? c const char s = ""; int n = 0; for (int i = 0; i < strlen(s) - 1; i++) { n++; if (n > 5) break; } printf("%d", n);
- What is printed? c int pm(int b, int e, int m) { if (e == 0) return 1 % m; int h = pm(b, e / 2, m); h = h h % m; return e % 2 ? h b % m…
- What is printed? c #define STR(x) #x printf("%zu", strlen(STR( a + b )));