- Assume most recent language standard are available (ES5, SE8, C11, C++11...).
- We expect good performance.
- If missing more requirements details, just make reasonable assumptions of
your own.
- Solution must be simple and compact.
No defensive coding, no comments, no unrequested features.
Only one file 10-20 lines of code
- Work only inside Google Docs: no external editor/IDE/debugger, no copy-paste
to/from such an editor. We must see the flow of how you write the code.
---------------------------------------------------------------------------------------------------------------
Implement function check (text) which checks whether brackets within text are
correctly nested. You need to consider brackets of three kinds: (), [], {}.
שיחה טלפונית שבסופה נשלח קישור לקובץ Google Docs בו שתי מטלות ב-JavaScript שעליך לבצע ב-30 דקות.
שאלות מתוך הראיון
1. בניית פונקציה אשר בודקת ומחזירה האם כמות הסוגריים במחרוזת תקינה (מבחינת פתיחות וסגירות), יש לטפל בשלושה סוגי סוגריים: (){}[]
2. פישוט קוד קיים, כלומר הפיכת פונקצייה גדולה ומסורבלת לפונקציה פשוטה שמכילה כ-5 שורות קוד. הפונקציה בודקת ומחזירה את המיקום הגדול ביותר של אחד משתי תווים במחרוזת מסויימת, אם אף אחד מהם לא קיים תחזיר הפונקציה 1-.
התקשרו ושלחו במייל תרגיל לעשות ולמלא בגוגל דוקס.
סהכ היה צריך לסיים תוך חצי שעה
שאלות מתוך הראיון
הכיל שתי חלקים
שאלה ראשונה לבדוק חוקיות בסוגריים מקוננות שאלה שניה ניתן קטע קוד שמומש באופן לא יעיל.
היה צריך לכתוב את אותו הקוד מבלי לשנות את מה שהוא עושה בבערך 5 שורות
Simplify the implementation below as much as you can.
Even better if you can also improve performance as part of the simplification!
FYI: This code is over 35 lines and over 300 tokens, but it can be written in
5 lines and in less than 60 tokens.
static int func(String s, char a, char b)
{
if (s.isEmpty()) return -1;
char[] strArray = string.toCharArray();
int i=0;
int aIndex=0;
int bIndex=0;
while (aIndex=0 && bIndex==0 && i
{
if (strArray[i] == a)
aIndex=i;
if (strArray[i] == b)
bIndex=i;
i++;
}
if (aIndex != 0)
{
if (bIndex == 0)
return aIndex;
else
return Math.min(a, b);
}
else
{
if (bIndex != 0)
return bIndex;
else
return -1;
}
}
תשובות
הוסף תשובה
|
לצפיה בתשובות
אוגוסט 2016
static int func(String s, char a, char b)
{
for (int i = 0; i < s.length(); ++i)
{
char ch = s.charAt(i);
if (ch == a || ch == b)
return i;
}