Skip to content

Instantly share code, notes, and snippets.

public class MagicSquare {
/* fields */
public int square[][];
/* constructor */
public MagicSquare(int size) {
this.square = new int[size][size];
for (int i = 0; i < square.length; i++)
for (int j = 0; j < square.length; j++)
@itayB
itayB / GoogleProtocolBuffers.md
Last active July 27, 2016 09:08
Protocol Buffers summary

Written by Itay Bittan.

Google Protocol Buffer

Background / Motivation

Most of our code project was written in C. Here are some of the pitfalls we suffered from:

  1. While working with separated modules/sub-projects for each component of the system, we had to maintain some shared place for most of the struct's definitions - so moving data from one module to another could be achieved (for example, via TCP sockets). It turned out that it is not a simple task and lead to a lot of dependencies between sub-projects, redundants headers files includes and sometimes even struct duplications.

  2. For persistency, some C structs were written into binary files (for example, query metadata). During the project lifecycle, these structs were changed (increased by more fields most of the time). Trying to read data from the old binaries files (which suitable to the old structs) cause malformed incorrect data and sometimes even caused to segmentation faults and system crash.

Written by Itay Bittan.

Apache Kafka

Background / Motivation

Our primary product contains a lot of modules (separate processes) talking together. At the beginning we wrote our own IMP (internal message protocol) written in C. It allowed us to send buffers (C structures) between modules over TCP connection. In addition, we developed a communication module - process that responsible to pass messages between all of the system modules. Here are some of the pitfalls we suffered from:

  1. Limited queue size - working with TCP sockets queues we find out that we where limited to relatively small size. If you want see your buffer size in terminal, you can take a look at: /proc/sys/net/ipv4/tcp_rmem (for read)
#include <stdio.h>
#include <iostream>
using namespace std;
/* Get string with number at the beginning of it and returns the number value. for example: "120ab" --> 120 */
int getNumber(char* str) {
int i = 0;
int size = strlen(str);
int value = 0;
#include <stdio.h>
class A
{
public:
A() { printf("A\n"); }
~A() { printf("~A\n"); }
void p1() { printf("p1A\n"); }
virtual void p2() { printf("p2A\n"); }
};
@itayB
itayB / ReverseWords.cpp
Last active August 13, 2016 09:47
Write a program to reverse the words order in a sentence (without using additional space)
#include <string>
void swap(char* a, char* b) {
char temp = *a;
*a = *b;
*b = temp;
}
void reverse_chars(char* str, int start, int end) {
// TODO: add nullptr validation
@itayB
itayB / GoogleInterview-WordsSeparation1.cpp
Last active February 19, 2021 18:37
Write a program that breaks up a string of words with no spaces into a string with the appropriate spaces
// This solution use another char array (which mean space complexity of O(n) where n in the length of the string)
#include <string>
#include <set>
#include <iostream>
using namespace std;
class Dictionary {
@itayB
itayB / MyHeritage.cpp
Created August 16, 2016 12:50
Compare names for smart matching
#include <string>
#include <set>
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
using namespace std;
void split(const string &s, char delim, vector<string> &elems) {
#include <iostream>
using namespace std;
#define MAX_A 10
#define MAX_B 4
void sortedMerge(int a[], int b[]) {
int pA = MAX_A - MAX_B - 1;
int pB = MAX_B - 1;
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <string>
#define MAX_A 10
using namespace std;