שאלות כדוגמת "כמה רכבים יש בישראל?". שאלות שבהן התשובה הסופית המספרית לא באמת חשובה, אלא הדרך ותהליך החשיבה. המראיין רוצה לראות כיצד המועמד מפרק את השאלה ומגיע לתשובה.
1. בהינתן מערך a בגודל n של מספרים תחזיר מערך באותו גודל שבתא a[i] נמצאת מכפלת כל איברי המערך חוץ מ-a[i].
2. Given 2 linked lists with common element find the common element
תשובות
הוסף תשובה
|
לצפיה בתשובות
ספטמבר 2017
1. הפיתרון הנאיבי הוא ברור (מצא מכפלת כל המספרים במערך שהוא, למשל, P, ואז בתא i יהיה P / a[i]), O(n) סיבוכיות.
פיתרון יותר מתוחכם:
void product_array2(int a[],int n)
{
int i;
int prod=1,prod_array[n];
/* we first store the product of all the numbers that lie to the left of a number in the array.
For the first element the product of elements to the left is 1 */
for(i=0;i
{ prod_array[i]=prod;
prod*=a[i];
}
prod=1;
/* We multiply the product on the right of a number to the already stored product which gave us the product of
numbers to the left . */
for(i=n-1;i>=0;i--)
{
prod_array[i]*=prod;
prod*=a[i];
}
for(i=0;i
{
cout<
}
cout<
}
2. If the list lengths are the same then advance until you meet, otherwise advance on the longest (long - small) steps then start advancing on both.