Skip to content

Instantly share code, notes, and snippets.

@dautermann
dautermann / SortAllZerosToEndOfArrayAndReturnCountOfNonZeroElements.m
Last active September 9, 2015 12:35
an algorithm question from a recent interview: given an array (for example, [ 1, 0, 2, 0, 0, 3, 4 ]), implement a method that: 1. returns the number of non-zero elements (4) 2. moves the non-zero elements to the beginning of the array (the rest of the elements don't matter) -> both [ 1, 2, 3, 4, 0, 0, 0] and [ 4, 1, 3, 2, X, Y, Z ] are valid
/*
Given an array (for example, [ 1, 0, 2, 0, 0, 3, 4 ]), implement a method that
1. returns the number of non-zero elements (4)
2. moves the non-zero elements to the beginning of the array (the rest of the elements don't matter)
-> both [ 1, 2, 3, 4, 0, 0, 0] and [ 4, 1, 3, 2, X, Y, Z ] are valid
*/
@interface FB : NSObject
@dautermann
dautermann / GetFontFamilyNameInSwift
Last active April 10, 2019 05:36
Drop this thing into your iOS app to get the family name of custom fonts your using... or you could use MacOS Font Book. At least this way you know the custom font made it to the app on your simulator or device.
for family in UIFont.familyNames {
Swift.print("family is \(family)")
let specificFamilyName = family as String
for fontName in UIFont.fontNames(forFamilyName: specificFamilyName)
{
Swift.print("family name is \(fontName)")
}
}
@dautermann
dautermann / ReverseCharactersInAnyString
Created November 13, 2014 03:51
Q) How does one reverse the characters in a string (e.g. “god” becomes “dog”). This classic computer science question can then be extended to the more interesting question: how does one reverse all the characters in a sentence, but keeping the actual words in place (e.g. “cat chases dog” becomes “dog chases cat”)? A) Here’s the full answer (and …
reverseString(char *array, int start, int end)
{
int rightIndex = end;
for(int leftIndex = start; leftIndex < rightIndex; leftIndex++)
{
char swapChar = array(rightIndex);
array(rightIndex) = array(leftIndex);
array(leftIndex) = swapChar;
rightIndex--;
}
@dautermann
dautermann / FindWinnerInTicTacToeAlgorithm
Last active February 25, 2019 02:33
One of my recent job interviews asked me to devise an algorithm to find a winner in a Tic-Tac-Toe grid. Because this was the first technical interview of that day, I was far too nervous or stressed about impressing the two dudes talking to me and I ended up not paying close enough attention to the white board while I was speaking pseudo-confiden…
My first attempt was a brute force attempt. I didn't even try to come up with the (time) complexity of this, at least while I was on-site.
// in Objective C everything is 0 (zero) based... but on my white board
// my tic-tac-toe grid object was 1 based...
// we might want to check if the game is valid even before
// we waste time looking to see if there's a winning set of three
- (BOOL) isValidGame: (Grid *) grid
{
int totX = 0;
@dautermann
dautermann / ObjCFileNameSorting
Created April 10, 2013 12:31
Given an array of strings, e.g.: NSArray * files = @"[@"file2", @"folder22", @"folder10", @"file1", @"folder100", … … …, nil]; Describe how you would write some kind of method or function that would sort the strings in a predictable manner (e.g. "file1", "file2", "folder2", "folder10", "folder100" instead of "folder10", "folder100", "folder2")
- (NSComparisonResult)fileNameCompare:(NSString *)aString
{
NSComparisonResult result;
// split string1 and string2 each into two halves
//
// then compare string1 and string2's halves with each other
NSString * firstHalfOfSelf = [NSString getFirstHalfOf: self];
NSString * firstHalfOfOtherString = [NSString getFirstHalfOf: aString];
@dautermann
dautermann / ObjCLinkedList
Created November 13, 2012 19:11
LinkedList implementation in Objective C
// instructions for actually running & trying this out are at the bottom of this file...
@interface LList : NSObject
{
NSInteger _currentValue;
LList * _next;
}
- (void) insert: (NSInteger) valueToInsert;
- (void) printValue;