Skip to content

Instantly share code, notes, and snippets.

@ThunderXu
ThunderXu / CareerCup1.1.cpp
Last active December 14, 2015 02:29
Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?
#include "stdafx.h"
#include <string>
#include <iostream>
bool HasDuplication(std::string);
int main()
{
using namespace std;
string str;
cin>>str;
@ThunderXu
ThunderXu / CareerCup1.2.cpp
Created February 23, 2013 01:35
Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string.
#include "stdafx.h"
#include <string>
#include <iostream>
void reverse(char*);
int main()
{
using namespace std;
char str[] = "This is a test";
@ThunderXu
ThunderXu / Careercup1.3.cpp
Created February 23, 2013 01:58
Given two strings, write a method to decide if one is a permutation of the other.
#include "stdafx.h"
#include <string>
#include <iostream>
const int SIZE = 256;
bool IsPermutation(std::string, std::string);
int main()
{
using namespace std;
@ThunderXu
ThunderXu / CareerCup1.4.cpp
Created February 23, 2013 02:31
Write a method to replace all spaces in a string with '%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so you can perform this operation in place.) EXAMPLE Inpu…
#include "stdafx.h"
#include <string>
#include <iostream>
void ReplaceSpace(std::string&);
int main()
{
using namespace std;
string str;
getline(cin,str);
@ThunderXu
ThunderXu / CareerCup1.5.cpp
Created February 23, 2013 02:41
Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2b1c5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string.
#include "stdafx.h"
#include <string>
#include <iostream>
#include <sstream>
std::string Compress(std::string);
int main()
{
using namespace std;
string originstr;
@ThunderXu
ThunderXu / CareerCup1.6.cpp
Last active December 14, 2015 03:09
Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?
#include <iostream>
#include <fstream>
bool ReadFile(int**, int);
void Output(int**, int);
void Rotate(int**, int, int);
int main()
{
using namespace std;
cout<<"Enter the size please"<<endl;
@ThunderXu
ThunderXu / CareerCup1.7.cpp
Created February 23, 2013 07:08
Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.
#include <iostream>
#include <fstream>
bool ReadFile(int**, int, int);
void Output(int**, int, int);
void SetZero(int**, int, int);
int main()
{
using namespace std;
cout<<"Enter the width and height please"<<endl;
@ThunderXu
ThunderXu / CareerCup1.8.cpp
Created February 24, 2013 07:49
Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (e.g., "waterbottle" is a rotation of "erbottlewat").
#include <iostream>
#include <string>
bool IsSubString(std::string, std::string);
bool IsRotation(std::string, std::string);
int main()
{
using namespace std;
string str1,str2;
cin>>str1>>str2;
@ThunderXu
ThunderXu / CareerCup2.1.cpp
Created February 24, 2013 09:58
Write code to remove duplicates from an unsorted linked list. FOLLOW UP How would you solve this problem if a temporary buffer is not allowed?
#include <iostream>
#include <hash_set>
#include <string>
class Node
{
public:
int data;
Node* next;
};
@ThunderXu
ThunderXu / CareerCup2.2.cpp
Created February 25, 2013 13:23
Implement an algorithm to find the kth to last element of a singly linked list.
#include <iostream>
#include <string>
class Node
{
public:
int data;
Node* next;
};