Skip to content

Instantly share code, notes, and snippets.

View lelandjansen's full-sized avatar
🐙
200

Leland Jansen lelandjansen

🐙
200
View GitHub Profile
@lelandjansen
lelandjansen / efficient-techniques.cc
Created December 7, 2018 16:48
Efficient vector and caching techniques
#include <algorithm>
#include <chrono>
#include <iostream>
#include <iterator>
#include <memory>
#include <random>
#include <unordered_map>
// Efficiency with Algorithms, Performance with Data Structures
// CppCon 2014: Chandler Carruth
@lelandjansen
lelandjansen / election.cc
Created July 18, 2018 04:07
Election interview question
// You are given a sorted array of numbers where each number represents a
// candidate in an election. For example, in the list below candidate 1 received
// 2 votes, candidate 2 received 1 vote, candidate 3 received 6 votes, etc.
// [1, 1, 2, 3, 3, 3, 3, 3, 3, 4, 5]
// A candidate wins an election if and only if they have strictly greater than
// 50% of the votes.
// Write a function to determine the winning candidate, if any.
#include <optional>
#include <vector>
@lelandjansen
lelandjansen / parity.cc
Last active September 30, 2016 19:38
Compute the parity of a number (two methods)
#include <iostream>
bool PairityA(int n) {
bool res = 0;
while(n) {
res ^= n & 1;
n >>= 1;
}
return res;
}
@lelandjansen
lelandjansen / is_permutation.cc
Created September 13, 2016 21:48
Determine if one string is a permutation of another
#include <string>
#include <map>
bool IsPermutation(std::string &a, std::string &b) {
std::map<char, std::pair<int, int>> char_map;
for (auto c : a) ++char_map[c].first;
for (auto c : b) ++char_map[c].second;
for (auto it : char_map)
if (it.second.first != it.second.second) return false;
return true;
@lelandjansen
lelandjansen / ReverseLinkedList.cc
Last active August 7, 2016 05:11
Reverse a singly-linked list
#include <iostream>
#include <vector>
struct Node {
char value;
Node *next;
};
class LinkedList {
public:
@lelandjansen
lelandjansen / StringReverse.cc
Created August 7, 2016 00:41
Reverse a string
#include <iostream>
#include <vector>
namespace String {
std::string Reverse(std::string str) {
char temp;
for (unsigned long i = 0; 2*i < str.length(); ++i) {
temp = str[i];
str[i] = str[str.length()-i-1];
str[str.length()-i-1] = temp;
@lelandjansen
lelandjansen / OddOccurrencesInArray.cpp
Last active June 26, 2021 09:10
Returns the value of the unpaired element in an array
int oddOccurrencesInArray(int A[], int N) {
int value = 0;
for (int i = 0; i < N; ++i) value ^= A[i];
return value;
}
// Source: https://codility.com/programmers/task/odd_occurrences_in_array/
// Score: 100%
// That's all folks!
@lelandjansen
lelandjansen / rotateArray.js
Created January 20, 2016 01:39
Rotates an array K times to the right
/*
Task description
A zero-indexed array A consisting of N integers is given. Rotation of the array
means that each element is shifted right by one index, and the last element of
the array is also moved to the first place.
For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7]. The
goal is to rotate array A K times; that is, each element of A will be shifted to
the right by K indexes.
@lelandjansen
lelandjansen / fizzBuzz.c
Created January 8, 2016 04:09
FizzBuzz
/*
fizzBuzz prints integers from 1 to max. For multiples of three, “Fizz” is
printed instead of the number. For multiples of five, “Buzz” is printed instead
of the number. For numbers which are multiples of both three and five,
“FizzBuzz” is printed instead of the number.
*/
#include <stdio.h>
void fizzBuzz(int max) {