Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,163,074 members, 7,852,659 topics. Date: Thursday, 06 June 2024 at 11:03 PM

Technical Interview - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Technical Interview (347 Views)

I Just Bombed A Technical Interview / Job Application - Back End Software Engineer (node.js) with technical Exam / Python for Non-Technical people (2) (3) (4)

(1) (Reply) (Go Down)

Technical Interview by LooneyLester: 6:54pm On Dec 31, 2023
Can you pass the Rockstar Games Job Interview?

Re: Technical Interview by Stephen0mozzy: 7:20pm On Dec 31, 2023
Thinking....
Re: Technical Interview by LikeAking: 9:50am On Jan 01
Very cheap
Re: Technical Interview by Karleb(m): 12:49am On Feb 17
I may be wrong o but let me just tell you what I have seen so far.

The second loop should run to i and not count.

BitCount method is supposed to take in integer but in the usage, a boolean is being passed.
It should be BitCount(intArray[i]) <= BitCount(intArray[j]).

I also think in the BitCount, i should be decremented like so i-- outside the if statement in the while loop so it doesn't loop forever.

I presume i>>=1 is the statement counting the bits, then it should be before the if statement and the count should be count += i.

Anyway, type that to CHATGPT and see what it gives.
Re: Technical Interview by airsaylongcome: 3:37am On Feb 17
Karleb:
I may be wrong o but let me just tell you what I have seen so far.

The second loop should run to i and not count.

BitCount method is supposed to take in integer but in the usage, a boolean is being passed.
It should be BitCount(intArray[i]) <= BitCount(intArray[j]).

I also think in the BitCount, i should be decremented like so i-- outside the if statement in the while loop so it doesn't loop forever.

I presume i>>=1 is the statement counting the bits, then it should be before the if statement and the count should be count += i.

Anyway, type that to CHATGPT and see what it gives.

Output from CHATGPT

Here's the corrected version of the code:


```cpp
#include <iostream>

// Function to count the number of set bits (1s) in an unsigned integer
unsigned int BitCount(unsigned int i) {
int count = 0;
while (i != 0) {
if (i & 1) { // Changed | to &
count++;
}
i >>= 1;
}
return count;
}

// Function to sort an array by the number of set bits in the binary representation of each number
void SortIntsByBitCount(unsigned int* intArray, int count) {
for (int i = 0; i < count; i++) { // Changed <= to <
for (int j = i + 1; j < count; j++) { // Changed <= to <
if (BitCount(intArray[i]) > BitCount(intArray[j])) { // Fixed comparison and added missing parameter
unsigned int temp = intArray[i];
intArray[i] = intArray[j];
intArray[j] = temp;
}
}
}
}

int main() {
unsigned int array[] = {5, 3, 7, 1, 9}; // Example array
int arraySize = sizeof(array) / sizeof(array[0]);

SortIntsByBitCount(array, arraySize);

// Output sorted array
std::cout << "Sorted array: ";
for (int i = 0; i < arraySize; i++) {
std::cout << array[i] << " ";
}
std::cout << std::endl;

return 0;
}
```

Changes made:

1. Corrected the bitwise OR (`|`) to a bitwise AND (`&`) in the `BitCount` function to properly count the number of set bits.
2. Fixed the missing curly braces and return statement in the `BitCount` function.
3. Fixed the loop conditions in the `SortIntsByBitCount` function to avoid going out of bounds and to properly compare elements.
4. Added missing parameter `intArray[j]` in the `BitCount` function call within the `SortIntsByBitCount` function.
5. Swapped elements correctly in the `SortIntsByBitCount` function when the number of set bits is different.
Re: Technical Interview by Karleb(m): 8:01am On Feb 17
airsaylongcome:


Output from CHATGPT

Here's the corrected version of the code:


```cpp
#include <iostream>

// Function to count the number of set bits (1s) in an unsigned integer
unsigned int BitCount(unsigned int i) {
int count = 0;
while (i != 0) {
if (i & 1) { // Changed | to &
count++;
}
i >>= 1;
}
return count;
}

// Function to sort an array by the number of set bits in the binary representation of each number
void SortIntsByBitCount(unsigned int* intArray, int count) {
for (int i = 0; i < count; i++) { // Changed <= to <
for (int j = i + 1; j < count; j++) { // Changed <= to <
if (BitCount(intArray[i]) > BitCount(intArray[j])) { // Fixed comparison and added missing parameter
unsigned int temp = intArray[i];
intArray[i] = intArray[j];
intArray[j] = temp;
}
}
}
}

int main() {
unsigned int array[] = {5, 3, 7, 1, 9}; // Example array
int arraySize = sizeof(array) / sizeof(array[0]);

SortIntsByBitCount(array, arraySize);

// Output sorted array
std::cout << "Sorted array: ";
for (int i = 0; i < arraySize; i++) {
std::cout << array[i] << " ";
}
std::cout << std::endl;

return 0;
}
```

Changes made:

1. Corrected the bitwise OR (`|`) to a bitwise AND (`&`) in the `BitCount` function to properly count the number of set bits.
2. Fixed the missing curly braces and return statement in the `BitCount` function.
3. Fixed the loop conditions in the `SortIntsByBitCount` function to avoid going out of bounds and to properly compare elements.
4. Added missing parameter `intArray[j]` in the `BitCount` function call within the `SortIntsByBitCount` function.
5. Swapped elements correctly in the `SortIntsByBitCount` function when the number of set bits is different.

Thank you. See as I embarrass myself up there. cheesy

It's important to run it and be sure it's giving the right result.

CHATGPT solutions are sometimes wrong.

(1) (Reply)

NUEVO GAMES: Upcoming Nigerian Game Developers / Pls Help. How Do I Get A Domain Name? How Do I Link My Website To The Web? / Crazytalk Animator 4 Mac Serial Number

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 18
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.