lucas
BAN USER- 0of 0 votes
AnswersAny feedback about my performance would be helpful.
I have my (unfinished) answer that I managed to code by the end of the interview. I also have my answer that I coded after the interview was over. Did I miss anything, or is there a simpler answer out there? I feel like I was pretty close, although I didn't finish, which feels pretty bad.
Palindrome question:
Is a string reversible?
Ignore illegal characters; ignore case. Only alphabetic characters are valid.
Here was my answer after ~45 mins:/* * Compare both ends for equality, meeting in the middle */ public static boolean isReversible(String s) { char[] chars = s.toCharArray(); int back = chars.length - 1; int front = 0; int middle = chars.length / 2; while (front < back) { char backChar = chars[back]; char frontChar = chars[front]; // check backChar as a alpha char while (backChar < ‘a’ || backChar > ‘z’) { back--; if (back < middle) return false; // Double check for multiple non-alpha chars middle--; backChar = chars[back]; } // check frontChar as a alpha char while (frontChar < ‘a’ || backChar > ‘z’) { front++; if (front >= middle) return false; // Double check for multiple non-alphas middle++; frontChar = chars[front]; } // We know that frontChar and backChar are both alpha // Check whether front and back are not past the middle if (frontChar != backChar) return false; front++; back--; } return true; }
Here was an answer that I came up with after the interview, which I believe is complete after manual testing:
- lucas in United States/* * Compare both ends for equality, meeting in the middle */ public static boolean isReversible(String s) { s = s.toLowerCase(); char[] chars = s.toCharArray(); int back = chars.length - 1; int front = 0; while (front < back) { char backChar = chars[back]; char frontChar = chars[front]; // check backChar as an alpha char while (backChar < 'a' || backChar > 'z') { back--; if (back < front) return true; backChar = chars[back]; } // check frontChar as an alpha char while (frontChar < 'a' || frontChar > 'z') { front++; if (front > back) return true; frontChar = chars[front]; } if (frontChar != backChar) return false; front++; back--; } return true; }
| Report Duplicate | Flag | PURGE
Algorithm
Here is the solution in Java:
// Both arrays are sorted and have the same size
public static double findMedianOfTwoArrays(int[] a1, int[] a2) {
// Since total size is 2N, median is average of (length/2)th and ((length/2)+1)th largest values
int median1 = 0;
int median2 = 0;
int i1 = 0;
int i2 = 0;
for (int i = 0; i < a1.length; i++) {
median1 = a1[i1] <= a2[i2] ? a1[i1++] : a2[i2++];
}
if (i1 >= a1.length)
median2 = a2[i2];
else if (i2 >= a2.length)
median2 = a1[i1];
else
median2 = a1[i1] <= a2[i2] ? a1[i1] : a2[i2];
return (median1 + median2) / 2.0;
}
Am I missing something, or is the example above (in Miguel's original answer) confusing because he is using two arrays that are NOT of equals sizes? Example:
Shouldn't these be equal sizes, as stated in the problem?
- lucas August 15, 2014