Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / EncryptDecrypt
Created February 6, 2016 12:42
You and your friend have a code language. To encode a message you replace every letter with another letter 2 ahead of it. E.g. “ENCODED MESSAGE” becomes “GPEQFGF OGUUCIG”. Input: “ENCODED MESSAGE” Output: “GPEQFGF OGUUCIG”
@interface EncryptDecrypt (NSString)
- (NSString *) encrypt;
- (NSString *) decrypt;
@end
@implementation EncryptDecrypt {
@dautermann
dautermann / RateMyAppLogic.m
Created February 17, 2016 21:55
If you were to show an app rating alert for active users of your app who have used your app at least on 3 unique days in the last 5 days. Please provide code for it.
// The way I would approach this problem would be to save a set of two distinct dates to
// NSUserDefaults and if these three days (the two days from the set plus today's third date) lie within the last five days,
// I'll show the alert. If we have two dates in the set, replace the oldest with the newest/current date.
// I'm using NSSet because it's distinct dates, versus an array where we might have duplicate dates.
// I'm making JetDate as a subclass of NSDate where the time component of NSDate (02-17-2015 00:00:00 GMTz)
// is dropped and only the date is considered
- (BOOL) shouldShowRatingAlert
@dautermann
dautermann / EnvoyMatrixCountElementsAroundBlock
Created October 24, 2016 13:42
Write a Swift playground that takes an n x n grid of integers. Each integer can be either 1 or 0. The playground then outputs an n x n grid where each block indicates the number of 1's around that block.
// Copyright © 2016 Envoy. All rights reserved.
/*
Write a Swift playground that takes an n x n grid of integers. Each integer can be either 1 or 0.
The playground then outputs an n x n grid where each block indicates the number of 1's around that block, (excluding the block itself) . For Block 0 on row 0, surrounding blocks are (0,1) (1,0) and (1,1). Similary for block (1,1) all the blocks around it are to be counted as surrounding blocks.
Requirements:
@dautermann
dautermann / WordDistanceFinder.swift
Created April 24, 2017 14:01
Given a list of words, provide a method that takes two words and returns the shortest distance (in words) between those two words in the provided text.
/* The following was a coding question given during an interview for LinkedIn in March, 2017 */
/* This class will be given a list of words (such as might be tokenized
* from a paragraph of text), and will provide a method that takes two
* words and returns the shortest distance (in words) between those two
* words in the provided text.
* Example:
* WordDistanceFinder finder = new WordDistanceFinder(Arrays.asList("the", "quick", "brown", "fox", "quick"));
* assert(finder.distance("fox", "the") == 3);
* assert(finder.distance("quick", "fox") == 1);
@dautermann
dautermann / TelephonePassphraseEquivalents
Last active October 19, 2017 02:16
Facebook Interview Question: Telephone Passphrase Equivalents
// A telephone keypad has letters associated with each number  (e.g. 2 = abc, 3 = def).
// Given a passphrase of "fb1" (e.g. one that you might use to log into a bank account),
// come up with an algorithm that would assemble an array that contains all the
// different possible letter combinations that, when typed into a telephone dial pad,
// would be equivalent to the original passphrase. That is, "fb1" equals "321" numerically;
// matching equivalent combinations include: "da1", "db1", "dc1", "ea1", "eb1", "ec1", "fa1" and "fc1".
// This can be dropped into an iOS Playground in Xcode
@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;
@dautermann
dautermann / AddAliasToDesktopUtility
Created January 9, 2018 12:34
create desktop alias to current macOS app
#import <Cocoa/Cocoa.h>
@interface AddAliasToDesktopUtility : NSObject {
}
- (BOOL) addAliasNow;
@end