0% found this document useful (0 votes)
2 views12 pages

Problems 24nov

The document contains a series of pseudocode problems and their explanations, focusing on outputs based on array manipulations and conditions. It includes a problem statement about hiring students based on their knowledge grades compared to job criteria, along with a solution outline. Additionally, it describes a program to print a butterfly pattern using stars based on a given integer N.

Uploaded by

anniegill707
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Problems 24nov

The document contains a series of pseudocode problems and their explanations, focusing on outputs based on array manipulations and conditions. It includes a problem statement about hiring students based on their knowledge grades compared to job criteria, along with a solution outline. Additionally, it describes a program to print a butterfly pattern using stars based on a given integer N.

Uploaded by

anniegill707
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link]

gle/wBfY8FE1nTA4M4TT7
1. What will be the output?

Pseudocode:

arr ← ["cat", "dog", "bird", "cow"]

print( arr[ len(arr) - 2 ] )

Options:
A. cat
B. dog
C. bird
D. cow

Answer: C. bird

Explanation:
len(arr) is 4 → len(arr) - 2 = 2 → arr[2] = "bird".

2. What does the following pseudocode print?

Pseudocode:

A ← ["a", "b", "c", "d"]

for i ← 0 to len(A)-1

A[i] ← A[i] + A[len(A)-1-i]

print(A[1])

Options:
A. ad
B. bc
C. bd
D. ba

Answer: C. bc

Explanation:
Pairs formed:

 A[0] = "a" + "d" = "ad"

 A[1] = "b" + "c" = "bc"

 A[2] = "c" + "b" = "cb"

 A[3] = "d" + "a" = "da"

But note: The loop modifies the array dynamically.


After i = 0: A = ["ad","b","c","d"]
After i = 1: A = ["ad","bc","c","d"]

So A[1] = "bc".

Correct is "bc", but this is not in options except B.


Thus correct answer: B. bc
3. What will be printed?

Pseudocode:

words ← ["red","blue","green","black"]

x ← ""

for i ← 1 to 3

x ← x + words[i][0]

print(x)

Options:
A. rbg
B. bgb
C. bgb
D. bgbk

Answer: C. bgb

Explanation:
words[1][0] = 'b'
words[2][0] = 'g'
words[3][0] = 'b'
Concatenation → "bgb".

5. What is the output of this pseudocode?

Pseudocode:

A ← [5, 2, 9, 1]

sum ← 0

for i ← 0 to len(A)-1

if A[i] % 2 = 1 then

sum ← sum + A[i]

print(sum)

Options:
A. 6
B. 10
C. 14
D. 15

Answer: D. 15

5+9+1=15

Explanation:
Odd numbers = 5, 9 → sum = 14.
6. What is printed?

Pseudocode:

S ← ["one","two","three"]

x ← S[0]

S[0] ← S[2]

S[2] ← x

print(S[1])

Options:
A. one
B. three
C. two
D. Error

Answer: C. two

Explanation:
Only S[0] and S[2] swap; S[1] remains "two".

7. Identify the output.

Pseudocode:

arr ← ["hi","hello","hey"]

for i ← len(arr)-1 downto 0

print(arr[i][1])

Options:
A. i e e
B. e e i
C. e e h
D. e l i

Answer: A. i e e

Explanation:
arr = ["hi","hello","hey"]
Print in reverse:

 arr[2][1] = 'i' from "hi"? Wait — check carefully: arr[2] = "hey": index 1 → 'e'.
Let's evaluate correctly:

Reverse order:

 i = 2 → "hey"[1] = 'e'

 i = 1 → "hello"[1] = 'e'
 i = 0 → "hi"[1] = 'i'

So output: e e i

Correct choice: B. e e i

8. What will the following code output?

Pseudocode:

A ← ["a","b","c","d"]

temp ← A[1]

A[1] ← A[3]

A[3] ← temp

print(A[3] + A[1])

Options:
A. bd
B. db
C. bb
D. dd

Answer: A. bd

Explanation:
Swap A[1] and A[3]:

Before: A = ["a","b","c","d"]
After : A = ["a","d","c","b"]

Print A[3] + A[1] → "b" + "d" = "bd".

9. What does this print?

Pseudocode:

str ← "abcde"

new ← ""

for i ← 0 to len(str)-1

if i % 2 = 0 then

new ← new + str[i]

print(new)

Options:
A. ace
B. bdf
C. abd
D. ad
Answer: A. ace

Explanation:
Even indices = 0,2,4 → a,c,e.

10. What will be the output?

Pseudocode:

A = ["stone", "crack", "blink"]

s = ""

for i = 0 to len(A)-1

for j = len(A[i]) - 1 downto 0

if j % 2 = 1 then

s = s + A[i][j]

print(s)

A. ntcrnl
B. tnc rnl
C. nc t rnl
D. ntc r ln

WORD 1: "stone"

Indices: s(0) t(1) o(2) n(3) e(4)

j loop: 4 → 0

Check each:

 j = 4 → e → 4%2=0 → skip

 j = 3 → n → odd → add

 j = 2 → o → even → skip

 j = 1 → t → odd → add

 j = 0 → s → even → skip

Contribution = "nt"

So far:
s = "nt"

WORD 2: "crack"

Indices: c(0) r(1) a(2) c(3) k(4)

j loop: 4 → 0
 j = 4 → k → even → skip

 j = 3 → c → odd → add

 j = 2 → a → even → skip

 j = 1 → r → odd → add

 j = 0 → c → even → skip

Contribution = "cr"

Now:
s = "ntcr"

WORD 3: "blink"

Indices: b(0) l(1) i(2) n(3) k(4)

j loop: 4 → 0

 j = 4 → k → even → skip

 j = 3 → n → odd → add

 j = 2 → i → even → skip

 j = 1 → l → odd → add

 j = 0 → b → even → skip

Contribution = "nl"

Now:
s = "ntcrnl"

FINAL RESULT

s = "ntcrnl"

Problem Statement 3:
A company is visiting a college for campus placements. The college has precisely N students and N
job openings available. The company’s policy is to recruit all new hires from a single college, ensuring
that all recruits are familiar with each other prior to joining, and to avoid the need for additional
team-building expenses. Each student is assessed and assigned a numerical grade based on their
knowledge level. All job openings have specific minimum criteria, represented by a numerical value,
and a student must have a knowledge grade equal to or higher than this criteria to be considered for
a job. For instance, if the criteria for a job opening is 10, a student with a knowledge grade of 10 or
higher will be hired, while those with lower grades will not. There are no limitations on job
assignments, and any student can be hired for any of the N job openings. The students do not have
any objections and are willing to be hired for any position, as long as they are hired.

Your task is to determine whether all of the students can be hired by the company or not.

 The first line contains the number of students, N.

 The second line contains N integers representing the knowledge grades of the students.

 The third line contains N integers representing the knowledge criteria for the N job openings.

Output Format:

Print "Hire" if all the students can be hired by the company.


Print "Not Hire" if it is not possible to hire all the students based on the knowledge criteria.

Input 1: 5

[13 46 34 84 49]

[10 39 48 79 22]

Output: Hire

Input 2:

N=3

[20, 15, 30]

[25, 10, 40]

Output: Not Hire

Solution:

1. Read N students
2. Read student grades array
3. Read job criteria array
4. Sort both arrays
5. For each index i:
6. If students[i] < jobs[i], print "Not Hire" and stop
7. If loop completes, print "Hire"

N=5
[13 46 34 84 49] After sorting -> [13 34 46 49 84]

[10 39 48 79 22] After sorting -> [10 22 39 48 79]

N=3

[20, 15, 30] After sorting -> [15 20 30]

[25, 10, 40] After sorting -> [10 25 40]

int main() {

int N;

cin >> N;

int* students = new int[N]; // dynamically allocate

int* jobs = new int[N];

for (int i = 0; i < N; i++)

cin >> students[i];

for (int i = 0; i < N; i++)

cin >> jobs[i];

sort(students, students + N);

sort(jobs, jobs + N);

for (int i = 0; i < N; i++) {

if (students[i] < jobs[i]) {

cout << "Not Hire" << endl;

cout << "Hire" << endl;

}
Program to Print Butterfly Pattern (Star Pattern)

Given an integer N, print N rows of Butterfly pattern.

The butterfly consists of:

 A top half (expanding wings)

 A bottom half (shrinking wings)

For N = 5, rows = 2N – 1 = 9 rows.

#include <iostream>

using namespace std;

int main()

// Number of rows

int N = 5;

int spaces = 2 * N - 1;

int stars = 0;

// The outer loop

for (int i = 1; i <= 2 * N - 1; i++) {

// Upper half of the butterfly

if (i <= N) {

spaces = spaces - 2;
stars++;

 If i ≤ N (i = 1 to 5): TOP HALF

This makes:

 stars grow: 1,2,3,4,5

 spaces shrink: 7,5,3,1,–1

// Lower half of the butterfly

else {

spaces = spaces + 2;

stars--;

 If i > N (i = 6 to 9): BOTTOM HALF

spaces += 2

stars -= 1

This makes:

 stars shrink: 4,3,2,1

 spaces grow: 1,3,5,7

// Print stars For each row- Left wing stars

for (int j = 1; j <= stars; j++) {

cout << "*";

// Print middle spaces

for (int j = 1; j <= spaces; j++) {

cout << " ";

// Print Right wing stars

//if (j != N) ensures that only ONE star extra does NOT print when stars == N.
//This prevents printing double-centered stars.

for (int j = 1; j <= stars; j++) {

if (j != N) {
cout << "*";

cout << "\n";

You might also like