Hey there, I saw a post on Reddit about a job application.
The poster would test the candidates with 5 programming problems. While it makes sense to check if ppl actually can programm I am not so sure wether Problem 4 and 5 are good job application Problems.
I basically wanted to dip my fingers in c++ and solved the first 4 problems, problem 5 is solveable and I’ve tried it but didn’t came up with a solution in a reasonable time.
Warning: It was more of a training to learn cpp, and sometimes it was written more complicated than needed because I wanted to try certain things. All code is written in Qt Creator!
Problem 1
Write three functions that compute the sum of the numbers in a given list using a for-loop, a while-loop, and recursion.
My solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | #include <QCoreApplication> #include <iostream> using namespace std; int numberSum( int * ptrList, int size); int numberSum2( int * ptrList, int size); int numberSum3( int * ptrList, int size); int main( int argc, char *argv[]) { QCoreApplication a(argc, argv); int numberList[] = {2, 5, 7, 3, 6, 6, 7}; int * ptrList = numberList; int arraySize = sizeof (numberList) / sizeof (*numberList); cout << numberSum(ptrList, arraySize); cout << numberSum2(ptrList, arraySize); cout << numberSum3(ptrList, arraySize); return a.exec(); } int numberSum( int * ptrList, int size) { int sum = 0; for ( int i = 0; i < size; i++) { sum += ptrList[i]; } cout << " ok " ; return sum; } int numberSum2( int * ptrList, int size) { int sum = 0; do { size--; sum += ptrList[size]; } while (size > 0); return sum; } int numberSum3( int * ptrList, int size) { int sum = 0; sum = ptrList[size-1]; if (size-1 > 0) { sum += numberSum3(ptrList, size -1); } return sum; } |
Problem 2
Write a function that combines two lists by alternatingly taking elements. For example: given the two lists [a, b, c] and [1, 2, 3], the function should return [a, 1, b, 2, c, 3].
My solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <QCoreApplication> #include <iostream> using namespace std; int main( int argc, char *argv[]) { QCoreApplication a(argc, argv); int list1[] = { 1, 2, 3, 4 }; char list2[] = { 'a' , 'b' , 'c' , 'd' }; int size = sizeof (list1)/ sizeof (*list1) + sizeof (list2)/ sizeof (*list2); char list3[ sizeof (list1)/ sizeof (*list1) + sizeof (list2)/ sizeof (*list2)]; int j = 0; for ( int i = 0; i < size; i++) { if (i % 2 == 0) { list3[i] = '0' + list1[j]; } else { list3[i] = list2[j]; j++; } } for ( int i = 0; i < size; i++) { cout << list3[i] << ", " ; } return a.exec(); } |
Problem 3
Write a function that computes the list of the first 100 Fibonacci numbers. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. As an example, here are the first 10 Fibonnaci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <QCoreApplication> #include <iostream> using namespace std; int main( int argc, char *argv[]) { QCoreApplication a(argc, argv); long long list[100] = {0, 1}; for ( int i = 2; i < 100; i++) { list[i] = list[i-1] + list[i-2]; } int size = sizeof (list); int i = 0; while (size > 0) { cout << list[i] << " " ; i++; size--; } return a.exec(); } |
Problem 4
Write a function that given a list of non negative integers, arranges them such that they form the largest possible number. For example, given [50, 2, 1, 9], the largest formed number is 95021.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include <QCoreApplication> #include <iostream> using namespace std; void bubblesort( int * array, int size); int main( int argc, char *argv[]) { QCoreApplication a(argc, argv); int array[] = { 1, 4, 5, 7, 1, 2 }; int * pArray = array; //cout << sizeof(array) / sizeof(*array); bubblesort(pArray, ( sizeof (array) / sizeof (*array))); for ( int i = 0; i < sizeof (array) / sizeof (*array); i++) { cout << array[i]; } return a.exec(); } void bubblesort( int * array, int size) { do { int j = 1; for ( int i = 0; i < size-1; i++) { if ( array[i] > array[i + 1]) { int temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; j = i+1; } } size = j; } while (size > 1); } |
Update: Apparently this problem got a lot of people talking (although not as much as Problem 5 below.) You can click here to read my solution.
Problem 5
Write a program that outputs all possibilities to put + or – or nothing between the numbers 1, 2, …, 9 (in this order) such that the result is always 100. For example: 1 + 2 + 34 – 5 + 67 – 8 + 9 = 100.
Update: (Here is one solution to this problem in case you are curious.)
Archives
- November 2023
- December 2022
- November 2022
- February 2022
- November 2021
- October 2021
- September 2021
- July 2021
- April 2021
- March 2021
- February 2021
- January 2021
- September 2020
- July 2020
- April 2020
- March 2020
- February 2020
- December 2019
- November 2019
- October 2019
- August 2019
- June 2019
- February 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- May 2018
- March 2018
- February 2018
- December 2017
- November 2017
- September 2017
- July 2017
- June 2017
- April 2017
- February 2017
- January 2017
- October 2016
- September 2016
- July 2016
- May 2016
- April 2016
- March 2016
- August 2015
- July 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- October 2014
- April 2014
- March 2014
- February 2014
Calendar
M | T | W | T | F | S | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |