diff --git a/NokiaInterviewTest.cpp b/NokiaInterviewTest.cpp new file mode 100644 index 0000000..6ffa2cb --- /dev/null +++ b/NokiaInterviewTest.cpp @@ -0,0 +1,239 @@ +/* Check cf5-opt.vim defs. +VIM: let g:lcppflags="-std=c++11 -O2 -pthread" +VIM: let g:wcppflags="/O2 /EHsc /DWIN32" +VIM: let g:argv="" +*/ +#include +#include +#include +#include +#include + +int testinput[] = { +1,0,0,0,1,0,0,0,0 +}; + +// find bug. +int solution3(std::vector& A) { + int n = A.size(); + int i = n - 1; + int result = -1; + int k = 0; + int maximal = 0; + while (i > 0) + { + if (A[i] == 1) + { + k = k + 1; + if (k >= maximal) + { + maximal = k; + result = i; + } + } + else + { + k = 0; + } + i = i - 1; + } + if (A[i] == 1 && k + 1 > maximal) // <- bug is here. there should be k+1 >= maximal + result = 0; + return result; +} + +const char * a1 = "abc"; +const char * a2 = "Abc"; + +int solution4(const std::string & s1, const std::string & s2) +{ + std::string::const_iterator s1it = s1.begin(); + std::string::const_iterator s1et = s1.end(); + std::string::const_iterator s2it = s2.begin(); + std::string::const_iterator s2et = s2.end(); + for ( ; s1it!=s1et && s2it!=s2et; ++s1it, ++s2it ) + { + char c1 = toupper(*s1it); + char c2 = toupper(*s2it); + if ( c1 < c2 ) + return -1; + else if ( c2 < c1 ) + return 1; + } + + if ( s1it==s1et && s2it==s2et ) + return 0; + else if ( s1it==s1et ) + return -1; + else + return 1; +} + +int solution2(int blx1, int bly1, int trx1, int try1, int blx2, int bly2, int trx2, int try2) +{ + if ( blx1 > trx1 ) + std::swap( blx1, trx1 ); + if ( bly1 > try1 ) + std::swap( bly1, try1 ); + if ( blx2 > trx2 ) + std::swap( blx2, trx2 ); + if ( bly2 > try2 ) + std::swap( bly2, try2 ); + + if ( trx1 < blx2 + || trx2 < blx1 + || try1 < bly2 + || try2 < bly1 ) + return 0; + + long long x[4] = { blx1,trx1,blx2,trx2 }; + std::sort( &x[0], &x[4]); + + long long y[4] = { bly1,try1,bly2,try2 }; + std::sort( &y[0], &y[4]); + + unsigned long long a = (unsigned long long)(x[2]-x[1])*(unsigned long long)(y[2]-y[1]); + return ( a <= 2147483647 ) ? (int)a : -1; +} + +int solution1( std::vector & a ) +{ + typedef std::vector v_t; + + std::sort(a.begin(),a.end()); + + long long s = 0; + + v_t::const_iterator it=a.begin(); + v_t::const_iterator et=a.end(); + v_t::const_iterator jt; + for ( jt = it; it != et && *it == *jt; ++it ); + long long ic = it - jt; + s += ic*(ic-1)/2; + + for ( ; it != et; ) + { + for ( jt = it; it != et && *it == *jt; ++it ); + long long pc = ic; + ic = it - jt; + s += ic*(ic-1)/2; + s += ic*pc; + } + + const long long lim = 100000000; + return ( s <= lim ) ? s : -1; +} + +// you can also use includes, for example: +int solution_demo1( std::vector & a ) +{ + if ( !a.size() ) + return -1; + + typedef std::vector::const_iterator iterator; + typedef std::vector ub_t; + + int s = 0; + ub_t ub; + for ( iterator it=a.begin(), ib=a.begin(), ie=a.end(); + it!=ie; ++it ) + { + int c = it-ib; + long long l = c-*it; + long long r = c+*it; + + ub_t::iterator jt = std::lower_bound( ub.begin(), ub.end(), l); + s += ub.end()-jt; + if ( s > 10000000 ) + return -1; + + jt = std::upper_bound( ub.begin(), ub.end(), r); + ub.insert(jt, r); + } + return s; +} + +// you can also use includes, for example: +int solution_demo2( const std::vector & a ) +{ + typedef std::vector::const_iterator iterator; + // + // There is no solution if array 'a' has no elements. However, + // per task condition 'a' is non-empty. So, I don't consider that + // case. + // + std::vector m( a.size(), false ); + // + // This is the index. + // + int cvrp = 0; + // + // Iterate over array and find the index that inserts + // element into m. That will be wanted index. + // + for ( iterator it=a.begin(), ib=a.begin(), ie=a.end(); + it!=ie; ++it ) + { + if ( !m[*it] ) + { + m[*it]=true; + cvrp = it - ib; + } + } + // + // Make it index. + // + return cvrp; +} + +int main ( void ) +{try{ + + +// std::cout << solution( 0,0,2,2, 1,1,3,3 ) << std::endl; +// std::cout << solution( -2147483648,-2147483648,2147483647,2147483647, -2147483640,-2147483640,2147483640,2147483640 ) << std::endl; + + +// std::cout << solution( std::string(a1), std::string(a2) ) << std::endl; + +#if 1 + int cnt = sizeof(testinput)/sizeof(testinput[0]); + std::vector a; + //std::cout << solution( a ) << std::endl; + + a.reserve(cnt); + for ( int i = 0; i < cnt; ++i ) + a.push_back(testinput[i]); + std::cout << solution3( a ) << std::endl; + +/* + { + std::vector b(100000,1); + std::cout << solution( b ) << std::endl; + } + { + std::vector b(10000,1); + std::cout << solution( b ) << std::endl; + } + { + std::vector b(1000,1); + std::cout << solution( b ) << std::endl; + } +*/ +#endif + + return 0; +} +catch ( const std::exception& e ) +{ + std::cerr << std::endl + << "std::exception(\"" << e.what() << "\")." << std::endl; + return 2; +} +catch ( ... ) +{ + std::cerr << std::endl + << "unknown exception." << std::endl; + return 1; +}} + diff --git a/cf5-opt.vim b/cf5-opt.vim index 0c9d02c..0e2dbd1 100644 --- a/cf5-opt.vim +++ b/cf5-opt.vim @@ -55,6 +55,24 @@ function! s:SetUpLibrariesWindows() let g:Ltbb=" /LIBPATH:\"".g:tbblib."\" tbb.lib" let g:tbbmalloc=" tbbmalloc.lib" let g:tbbmproxy=" tbbmalloc_proxy.lib" + " + " Win SDK + " + let g:wsdkdir="C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A" + let g:wsdkinc=g:wsdkdir."\\Include" + let g:wsdklib=g:wsdkdir."\\Lib\\x64" + let g:wsdkbin=g:wsdkdir."\\Bin" + let g:Iwsdk=" /I\"".g:wsdkinc."\"" + let g:Lwsdk=" /LIBPATH:\"".g:wsdklib."\"" + " + " OpenGL + " + let g:wsdkdir="C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A" + let g:glinc=g:wsdkinc + let g:gllib=g:wsdklib + let g:glbin="" + let g:Igl=" /I\"".g:glinc."\"" + let g:Lgl=" /LIBPATH:\"".g:gllib."\" OpenGL32.Lib GLU32.lib" endfunction function! SetUpLibraries() diff --git a/exception_constructor.cpp b/exception_constructor.cpp new file mode 100644 index 0000000..ac037ee --- /dev/null +++ b/exception_constructor.cpp @@ -0,0 +1,54 @@ +/* Check cf5-opt.vim defs. +VIM: let g:lcppflags="-std=c++11 -O2 -pthread" +VIM: let g:wcppflags="/O2 /EHsc /DWIN32" +VIM-: let g:cf5output=0 +*/ +#include +#include +#include + +struct A +{ + A() + { + std::cout << "A::A()" << std::endl; + } + ~A() + { + std::cout << "A::~A()" << std::endl; + } +}; + +struct B : public A +{ + B() + { + std::cout << "B::B()" << std::endl; + } + ~B() + { + throw std::runtime_error("test"); + std::cout << "B::~B()" << std::endl; + } +}; + +int main ( void ) +{try{ + + B o; + + return 0; +} +catch ( const std::exception& e ) +{ + std::cerr << std::endl + << "std::exception(\"" << e.what() << "\")." << std::endl; + return 2; +} +catch ( ... ) +{ + std::cerr << std::endl + << "unknown exception." << std::endl; + return 1; +}} + diff --git a/memory_test_vahagnk-e6530.xlsx b/memory_test_vahagnk-e6530.xlsx new file mode 100644 index 0000000..4fe1589 Binary files /dev/null and b/memory_test_vahagnk-e6530.xlsx differ diff --git a/pod_constructor_optimization_for_array.cpp b/pod_constructor_optimization_for_array.cpp new file mode 100644 index 0000000..c7f5225 --- /dev/null +++ b/pod_constructor_optimization_for_array.cpp @@ -0,0 +1,37 @@ +#include +#include + +struct A +{ + int a; + int b; + + A() + { + std::cout << "A::A()" << std::endl; + } +}; + + +int main ( void ) +{try{ + + A a[4]; + + + + return 0; +} +catch ( const std::exception& e ) +{ + std::cerr << std::endl + << "std::exception(\"" << e.what() << "\")." << std::endl; + return 2; +} +catch ( ... ) +{ + std::cerr << std::endl + << "unknown exception." << std::endl; + return 1; +}} + diff --git a/tbb_test/main.cpp b/tbb_test/main.cpp deleted file mode 100644 index 290c50f..0000000 --- a/tbb_test/main.cpp +++ /dev/null @@ -1,132 +0,0 @@ -#include -#include -#include -#include - -#include -#include - -static const size_t N = 23; - -class SubStringFinder -{ - const std::string str; - size_t *max_array; - size_t *pos_array; - -public: - void operator() ( const tbb::blocked_range& r ) const - { - std::cout << "gs:" << r.grainsize() << " b:" << r.begin() << " e:" << r.end() << std::endl; - std::cout.flush(); - for ( size_t i = r.begin(); i != r.end(); ++i ) - { - size_t max_size = 0, max_pos = 0; - for (size_t j = 0; j < str.size(); ++j) - if (j != i) - { - size_t limit = str.size()-std::max(i,j); - for (size_t k = 0; k < limit; ++k) - { - if (str[i + k] != str[j + k]) - break; - if (k > max_size) - { - max_size = k; - max_pos = j; - } - } - } - max_array[i] = max_size; - pos_array[i] = max_pos; - } - } - - SubStringFinder(std::string &s, size_t *m, size_t *p) - : str(s), max_array(m), pos_array(p) - { } -}; - -int test() -{ - std::string str[N] = { std::string("a"), std::string("b") }; - - for (size_t i = 2; i < N; ++i) - str[i] = str[i-1]+str[i-2]; - std::string &to_scan = str[N-1]; - size_t num_elem = to_scan.size(); - - size_t *max = new size_t[num_elem]; - size_t *pos = new size_t[num_elem]; - - tbb::parallel_for( tbb::blocked_range(0, num_elem ), - SubStringFinder( to_scan, max, pos ) ); - - for (size_t i = 0; i < num_elem; ++i) - std::cout << " " << max[i] << "(" << pos[i] << ")" << std::endl; - delete[] pos; - delete[] max; - return 0; -} - -class copy_tracker -{ - int v; -public: - copy_tracker() - : v(0) - { - std::cout << "copy_tracker::copy_tracker()" << std::endl; - } - - copy_tracker( copy_tracker& c ) - : v( c.v ) - { - std::cout << "copy_tracker::copy_tracker( copy_tracker& c )" << std::endl; - } - - void echo() - { - std::cout << v << std::endl; - } -}; - -copy_tracker copy_tracker_by_value() -{ - return copy_tracker(); -} - -copy_tracker copy_tracker_by_value2() -{ - return copy_tracker_by_value(); -} - -void test_copy_tracker() -{ - volatile copy_tracker o = copy_tracker_by_value2(); - -} - -int test_flowgraph() -{ - return 0; -} - -int main ( void ) -{try{ - - test(); - - test_copy_tracker(); - return 0; -} -catch ( const std::exception& e ) -{ - std::cerr << std::endl - << "std::exception(\"" << e.what() << "\")." << std::endl; -} -catch ( ... ) -{ - std::cerr << std::endl - << "unknown exception." << std::endl; -}} \ No newline at end of file diff --git a/tbb_test/tbb_test.bat b/tbb_test/tbb_test.bat deleted file mode 100644 index 691ac9f..0000000 --- a/tbb_test/tbb_test.bat +++ /dev/null @@ -1,37 +0,0 @@ -cd "%~dp0" - -call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat" amd64 - -set SRCDIR=%CD%\src - -set BOOST=C:\Users\Vahagnk\src\boost_1_53_0 -set BOOST_INCLUDE=%BOOST% -set BOOST_LIB64=%BOOST%\stage\win64_vc12\lib - -set GTEST=C:\Users\Vahagnk\src\gtest-1.6.0 -set GTEST_INCLUDE=%GTEST%\include -set GTEST_LIB64=%GTEST%\build_x64_vc12 - -set ZLIB=C:\Users\Vahagnk\src\zlib-1.2.7 -set ZLIB_INCLUDE=%ZLIB% -set ZLIB_LIB64=%ZLIB%\build_x64_vc12 - -set LIBPNG=C:\Users\Vahagnk\src\libpng-1.5.10 -set LIBPNG_INCLUDE=%LIBPNG% -set LIBPNG_LIB64=%LIBPNG%\build_x64_vc10 - -set TBB=C:\Users\Vahagnk\src\tbb41_20121003oss -set TBB_INCLUDE=%TBB%\include -set TBB_LIB64=%TBB%\lib\intel64\vc11 -set TBB_BIN64=%TBB%\bin\intel64\vc11 - -set SRCDIR="%~dp0\src" - -set CL=/I%BOOST_INLCUDE% /I%TBB_INLCUDE% /I%GTEST_INCLUDE% /I%LIBPNG_INCLUDE% /I%ZLIB_INCLUDE% /I%QT_INCLUDE% /I%SRCDIR% /D_CRT_SECURE_NO_WARNINGS /DTEST_VERSION=\"vahagn_experimental\" -set LINK=/INCREMENTAL:NO - -set PATH=%TBB_BIN64%;%PATH% - -start devenv.exe %~n0.sln - - diff --git a/tbb_test/tbb_test.sln b/tbb_test/tbb_test.sln deleted file mode 100644 index 165382f..0000000 --- a/tbb_test/tbb_test.sln +++ /dev/null @@ -1,20 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "tbb_test", "tbb_test.vcxproj", "{5771BE40-BCC1-4DBB-9E0B-9B0FEF85927E}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Release|x64 = Release|x64 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {5771BE40-BCC1-4DBB-9E0B-9B0FEF85927E}.Debug|x64.ActiveCfg = Debug|x64 - {5771BE40-BCC1-4DBB-9E0B-9B0FEF85927E}.Debug|x64.Build.0 = Debug|x64 - {5771BE40-BCC1-4DBB-9E0B-9B0FEF85927E}.Release|x64.ActiveCfg = Release|x64 - {5771BE40-BCC1-4DBB-9E0B-9B0FEF85927E}.Release|x64.Build.0 = Release|x64 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/tbb_test/tbb_test.vcxproj b/tbb_test/tbb_test.vcxproj deleted file mode 100644 index 0e080f8..0000000 --- a/tbb_test/tbb_test.vcxproj +++ /dev/null @@ -1,159 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {5771BE40-BCC1-4DBB-9E0B-9B0FEF85927E} - Win32Proj - tbb_test - - - - Application - true - Unicode - v110 - - - Application - true - Unicode - v110 - - - Application - false - true - Unicode - v110 - - - Application - false - true - Unicode - v110 - - - - - - - - - - - - - - - - - - - false - - - false - - - false - - - false - - - - - - Level3 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - $(TBB_INCLUDE) - - - Console - true - $(TBB_LIB64) - tbb_debug.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) - - - - - - - Level3 - Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - $(TBB_INCLUDE) - - - Console - true - $(TBB_LIB64) - tbb_debug.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) - - - - - Level3 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - $(TBB_INCLUDE) - - - Console - true - true - true - $(TBB_LIB64) - tbb.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) - - - - - Level3 - - - MaxSpeed - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - $(TBB_INCLUDE) - - - Console - true - true - true - $(TBB_LIB64) - tbb.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) - - - - - - - - - \ No newline at end of file diff --git a/tbb_test/tbb_test.vcxproj.filters b/tbb_test/tbb_test.vcxproj.filters deleted file mode 100644 index 8755a3a..0000000 --- a/tbb_test/tbb_test.vcxproj.filters +++ /dev/null @@ -1,22 +0,0 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hpp;hxx;hm;inl;inc;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - Source Files - - - \ No newline at end of file